Skip to content

Commit ea9359f

Browse files
author
jinbeom
committed
Spiral Matrix Solution
1 parent 6d5ba91 commit ea9359f

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

longest-increasing-subsequence/kayden.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ def lengthOfLIS(self, nums: List[int]) -> int:
1313
else:
1414
path.append(num)
1515

16-
return len(path)
16+
return len(path)

spiral-matrix/kayden.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# 시간복잡도: O(N*M)
2+
# 공간복잡도: O(N*M)
3+
class Solution:
4+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
5+
dx = [0, 1, 0, -1]
6+
dy = [1, 0, -1, 0]
7+
n = len(matrix[0])
8+
m = len(matrix)
9+
visited = [[False] * n for _ in range(m)]
10+
visited[0][0] = True
11+
answer = []
12+
13+
14+
def dfs(x, y, direction):
15+
answer.append(matrix[x][y])
16+
nx = x + dx[direction]
17+
ny = y + dy[direction]
18+
19+
20+
if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]:
21+
visited[nx][ny] = True
22+
return dfs(nx, ny, direction)
23+
24+
direction = (direction+1) % 4
25+
nx = x + dx[direction]
26+
ny = y + dy[direction]
27+
28+
if 0 <= nx < m and 0 <= ny < n and not visited[nx][ny]:
29+
visited[nx][ny] = True
30+
return dfs(nx, ny, direction)
31+
else:
32+
return answer
33+
34+
return dfs(0, 0, 0)

0 commit comments

Comments
 (0)