From ace8eca8379025f0b6838e4e0855c21f98f41810 Mon Sep 17 00:00:00 2001 From: SinnoLn Date: Tue, 3 Nov 2026 22:45:11 +0900 Subject: [PATCH] 1914. Cyclically Rotating a Grid --- .../1914. Cyclically Rotating a Grid.java" | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 "leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" diff --git "a/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" "b/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" new file mode 100644 index 00000000..63a24f35 --- /dev/null +++ "b/leetcode3/\354\235\264\354\247\204\355\235\254/1914. Cyclically Rotating a Grid.java" @@ -0,0 +1,70 @@ +/* + +1. 아이디어 : 방문 배열과 이동 방향을 미리 정의 후, i,i부터 아래 -> 오른쪽 -> 위 -> 왼쪽 경로의 원소 위치 수집 + k번 만큼 회전(이동)한 위치에 넣는다 - 모듈러 연산 필요 + + +2. 시간복잡도 : O(2*N*M) + +3. 자료구조/알고리즘 : 방향 전환, 배열 + + */ + +class Solution { + private boolean[][] visited; + private int[][] ans; + private int[] dy = {0,1,0,-1}; + private int[] dx = {-1,0,1,0}; + public int[][] rotateGrid(int[][] grid, int k) { + // m, n은 짝수 + // k 횟수 만큼 왼쪽 회전 + + visited = new boolean[grid.length][grid[0].length]; + ans = new int[grid.length][grid[0].length]; + int limit = Math.min(grid.length, grid[0].length)/2; + for(int i=0; i list = new ArrayList<>(); + visited[y][x] = true; + list.add(new int[]{y,x}); + + int startY = y; + int startX = x; + + // 아래 방향 + int dir = 1; + while(true) { + int ny = y+dy[dir]; + int nx = x+dx[dir]; + + if(ny == startY && nx == startX) break; + + if(ny<0 || nx<0 || ny>=grid.length || nx>=grid[0].length || visited[ny][nx]) { + dir=(dir+1)%4; + continue; + } + + y = ny; + x = nx; + + visited[y][x] = true; + list.add(new int[]{y,x}); + } + + int size = list.size(); + int move = k%size; + + for(int i=0; i