|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | + |
| 4 | +public class Geegong { |
| 5 | + |
| 6 | + /** |
| 7 | + * direction vector 와 vistited 배열을 이용해 메모이제이션을 활용하여 풀어보기 |
| 8 | + * time complexity : o(n) |
| 9 | + * space complexity : o(n) - result 용 |
| 10 | + * @param matrix |
| 11 | + * @return |
| 12 | + */ |
| 13 | + // move Colum direction , move Row Direction |
| 14 | + public int[][] directionVectors = {{0,1}, {1,0}, {0,-1}, {-1, 0}}; |
| 15 | + public List<Integer> spiralOrder(int[][] matrix) { |
| 16 | + |
| 17 | + List<Integer> result = new ArrayList<>(matrix.length * matrix[0].length); |
| 18 | + |
| 19 | + // for memoization, 방문한 matrix의 인덱스들을 저장 |
| 20 | + // int 배열은 기본으로 0 이 초기화 |
| 21 | + int[][] visited = new int[matrix.length][matrix[0].length]; |
| 22 | + |
| 23 | + makeSpiralMatrix(matrix, 0,0, 0, result, visited); |
| 24 | + |
| 25 | + return result; |
| 26 | + } |
| 27 | + |
| 28 | + public void makeSpiralMatrix(int[][] matrix, int startRowIdx, int startColIdx, int directionOrder, List<Integer> result, int[][] visited) { |
| 29 | + if (startRowIdx < 0 || startRowIdx >= matrix.length || startColIdx < 0 || startColIdx >= matrix[0].length) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + // 방문했던 녀석을 다시 찾아온거라면 다 돌았다고 판단하고 recursive 종료 |
| 34 | + if (visited[startRowIdx][startColIdx] == 1) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // 이번 순서의 vector 를 가져온다 |
| 39 | + int[] direction = directionVectors[directionOrder]; |
| 40 | + |
| 41 | + // row, col 각각 어떤 방향으로 움직일지 지정 |
| 42 | + int moveRowPos = direction[0]; |
| 43 | + int moveColPos = direction[1]; |
| 44 | + |
| 45 | + int count = 0; |
| 46 | + |
| 47 | + int rowIndex = startRowIdx + (moveRowPos * count); |
| 48 | + int colIndex = startColIdx + (moveColPos * count); |
| 49 | + |
| 50 | + do { |
| 51 | + int currentVal = matrix[rowIndex][colIndex]; |
| 52 | + result.add(currentVal); |
| 53 | + visited[rowIndex][colIndex] = 1; |
| 54 | + |
| 55 | + count++; |
| 56 | + rowIndex = startRowIdx + (moveRowPos * count); |
| 57 | + colIndex = startColIdx + (moveColPos * count); |
| 58 | + if (colIndex < 0 || colIndex >= matrix[0].length || rowIndex < 0 || rowIndex >= matrix.length) { |
| 59 | + break; |
| 60 | + } |
| 61 | + |
| 62 | + } while(visited[rowIndex][colIndex] != 1); |
| 63 | + |
| 64 | + // vector 리스트들은 0번째, 1번째, 2번째, 3번째, 0번째, 1번째 .. 이 순서대로 꺼내서 사용한다 |
| 65 | + int nextDirectionOrder = directionOrder + 1 >= directionVectors.length ? 0 : directionOrder + 1; |
| 66 | + // 다음 방향을 미리 지정 |
| 67 | + int[] nextDirection = directionVectors[nextDirectionOrder]; |
| 68 | + |
| 69 | + // 다음 방향의 시작점 미리 지정 |
| 70 | + count--; |
| 71 | + int nextStartRowIdx = startRowIdx + (moveRowPos * count) + nextDirection[0]; |
| 72 | + int nextStartColIdx = startColIdx + (moveColPos * count) + nextDirection[1]; |
| 73 | + |
| 74 | + makeSpiralMatrix(matrix, nextStartRowIdx, nextStartColIdx, nextDirectionOrder, result, visited); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | + |
0 commit comments