https://leetcode.com/problems/spiral-matrix-iii/description/?envType=daily-question&envId=2024-08-08
You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.
You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid's boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.
Return an array of coordinates representing the positions of the grid in the order you visited them.
Example 1:
Input: rows = 1, cols = 4, rStart = 0, cStart = 0
Output: [[0,0],[0,1],[0,2],[0,3]]
Example 2:
Input: rows = 5, cols = 6, rStart = 1, cStart = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
Constraints:
- 1 <= rows, cols <= 100
- 0 <= rStart < rows
- 0 <= cStart < cols
M으로 되어있지만, 극악의 난이도다.
머리가 꼬이는 기분..
빠른 포기다
1.C
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
int directions[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int** spiralMatrixIII(int rows, int cols, int rStart, int cStart, int* returnSize, int** returnColumnSizes) {
int totalCells = rows * cols;
int **result = (int **)malloc(totalCells * sizeof(int *));
*returnColumnSizes = (int *)malloc(totalCells * sizeof(int));
for (int i = 0; i < totalCells; i++) {
result[i] = (int *)malloc(2 * sizeof(int));
(*returnColumnSizes)[i] = 2;
}
int direction = 0; // 현재 방향 인덱스
int steps = 1; // 현재 이동해야 하는 거리
int index = 0; // result 배열에 넣을 인덱스
result[index][0] = rStart;
result[index][1] = cStart;
index++;
while (index < totalCells) {
for (int i = 0; i < 2; i++) { // 각 거리 길이를 두 번 반복
for (int j = 0; j < steps; j++) {
rStart += directions[direction][0];
cStart += directions[direction][1];
if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) {
result[index][0] = rStart;
result[index][1] = cStart;
index++;
}
}
direction = (direction + 1) % 4; // 다음 방향으로 전환
}
steps++;
}
*returnSize = totalCells;
return result;
}
2. Python
class Solution(object):
def spiralMatrixIII(self, rows, cols, rStart, cStart):
i,j = rStart, cStart
diri, dirj = 0, 1 # directions to move
twice = 2
res = []
moves = 1
next_moves = 2
while len(res) < (rows * cols):
if (-1 < i < rows) and ( -1 < j < cols):
res.append([i,j])
i += diri
j += dirj
moves -= 1
if moves == 0:
diri, dirj = dirj, -diri # 90 deg Clockwise
twice -= 1
if twice == 0:
twice = 2
moves = next_moves
next_moves += 1
else:
moves = next_moves - 1
return res
'Coding_Practice' 카테고리의 다른 글
Remove Nth Node From End of List[M,Linked List,Two Pointers] (0) | 2024.08.12 |
---|---|
Search in Rotated Sorted Array[M,Array,Binary Search] (0) | 2024.08.11 |
Plus One[E,Array,Math] (0) | 2024.08.07 |
Linked List Cycle II[M,Hash Table,Linked List,Two Pointers] (0) | 2024.08.07 |
Remove Linked List Elements[E,Linked List,Recursion] (0) | 2024.08.06 |