|
| 1 | +/** |
| 2 | + * 2차 풀이: 기존 matrix 변경 없도록 개선 |
| 3 | + * |
| 4 | + * TC: O(ROW * COLUMN) |
| 5 | + * matrix 전체 순회 1회 |
| 6 | + * |
| 7 | + * SC: O(ROW * COLUMN) |
| 8 | + * 정답 제출을 위한 result 공간복잡도 |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {number[][]} matrix |
| 13 | + * @return {number[]} |
| 14 | + */ |
| 15 | +var spiralOrder = function (matrix) { |
| 16 | + const ROW = matrix.length; |
| 17 | + const COLUMN = matrix[0].length; |
| 18 | + // 1. 상하좌우 시작끝 index를 저장함 |
| 19 | + const boundary = { |
| 20 | + top: 0, |
| 21 | + bottom: ROW - 1, |
| 22 | + left: 0, |
| 23 | + right: COLUMN - 1, |
| 24 | + }; |
| 25 | + const result = []; |
| 26 | + |
| 27 | + while (result.length < ROW * COLUMN) { |
| 28 | + // 2. 오른쪽으로 순회 |
| 29 | + for (let column = boundary.left; column <= boundary.right; column++) { |
| 30 | + result.push(matrix[boundary.top][column]); |
| 31 | + } |
| 32 | + boundary.top += 1; |
| 33 | + |
| 34 | + // 3. 아래로 순회 |
| 35 | + for (let row = boundary.top; row <= boundary.bottom; row++) { |
| 36 | + result.push(matrix[row][boundary.right]); |
| 37 | + } |
| 38 | + boundary.right -= 1; |
| 39 | + |
| 40 | + // 4. 모두 순회했는데 왔던길 되돌아가는 경우를 막기위해 중간 조건문 추가 |
| 41 | + if (result.length === ROW * COLUMN) { |
| 42 | + break; |
| 43 | + } |
| 44 | + |
| 45 | + // 5. 왼쪽으로 순회 |
| 46 | + for (let column = boundary.right; column >= boundary.left; column--) { |
| 47 | + result.push(matrix[boundary.bottom][column]); |
| 48 | + } |
| 49 | + boundary.bottom -= 1; |
| 50 | + |
| 51 | + // 6. 위쪽으로 순회 |
| 52 | + for (let row = boundary.bottom; row >= boundary.top; row--) { |
| 53 | + result.push(matrix[row][boundary.left]); |
| 54 | + } |
| 55 | + boundary.left += 1; |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * 1차 풀이 |
| 63 | + * |
| 64 | + * TC: O(ROW * COLUMN) |
| 65 | + * matrix 전체 순회 1회 |
| 66 | + * |
| 67 | + * SC: O(ROW * COLUMN) |
| 68 | + * 정답 제출을 위한 result 공간복잡도 |
| 69 | + */ |
| 70 | + |
| 71 | +/** |
| 72 | + * @param {number[][]} matrix |
| 73 | + * @return {number[]} |
| 74 | + */ |
| 75 | +var spiralOrder = function (matrix) { |
| 76 | + const ROW = matrix.length; |
| 77 | + const COLUMN = matrix[0].length; |
| 78 | + // 우하좌상 순서 |
| 79 | + const DIRECTION = [ |
| 80 | + { |
| 81 | + row: 0, |
| 82 | + column: 1, |
| 83 | + }, |
| 84 | + { |
| 85 | + row: 1, |
| 86 | + column: 0, |
| 87 | + }, |
| 88 | + { |
| 89 | + row: 0, |
| 90 | + column: -1, |
| 91 | + }, |
| 92 | + { |
| 93 | + row: -1, |
| 94 | + column: 0, |
| 95 | + }, |
| 96 | + ]; |
| 97 | + |
| 98 | + // 1. 첫 시작점 방문표시 |
| 99 | + const result = [matrix[0][0]]; |
| 100 | + matrix[0][0] = "#"; |
| 101 | + |
| 102 | + let current = { |
| 103 | + row: 0, |
| 104 | + column: 0, |
| 105 | + }; |
| 106 | + let directionIndex = 0; |
| 107 | + |
| 108 | + // 2. 총 갯수만큼 채워질때까지 순회 |
| 109 | + while (result.length < ROW * COLUMN) { |
| 110 | + const next = { |
| 111 | + row: current.row + DIRECTION[directionIndex].row, |
| 112 | + column: current.column + DIRECTION[directionIndex].column, |
| 113 | + }; |
| 114 | + // 3. 다음 순회할 곳이 유효한 좌표인지 방문한 곳인지 확인 |
| 115 | + if ( |
| 116 | + !isValidPosition(next.row, next.column) || |
| 117 | + matrix[next.row][next.column] === "#" |
| 118 | + ) { |
| 119 | + // 4. 방향 전환 |
| 120 | + directionIndex = (directionIndex + 1) % 4; |
| 121 | + } else { |
| 122 | + // 5. 방문 표시 후 다음 좌표로 이동 |
| 123 | + result.push(matrix[next.row][next.column]); |
| 124 | + matrix[next.row][next.column] = "#"; |
| 125 | + current = next; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + return result; |
| 130 | + |
| 131 | + function isValidPosition(row, column) { |
| 132 | + if (row < 0 || ROW <= row) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + if (column < 0 || COLUMN <= column) { |
| 136 | + return false; |
| 137 | + } |
| 138 | + return true; |
| 139 | + } |
| 140 | +}; |
0 commit comments