File tree Expand file tree Collapse file tree 2 files changed +35
-1
lines changed
longest-increasing-subsequence Expand file tree Collapse file tree 2 files changed +35
-1
lines changed Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments