Skip to content

Commit cda9b06

Browse files
committed
unique-paths 공간 복잡도 최적화 sol (py)
1 parent 0e32e92 commit cda9b06

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

unique-paths/hi-rachel.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
풀이 시간: 16분
1111
처음에 어떻게 풀어야 할 줄 몰랐지만, 그림을 그려보며 누적 규칙을 찾음 (위, 왼쪽 값 더해나가기)
12+
paths[i][j] = paths[i-1][j] + paths[i][j-1]
1213
1314
TC: O(m * n)
1415
SC: O(m * n)
@@ -27,3 +28,24 @@ def uniquePaths(self, m: int, n: int) -> int:
2728
paths[i][j] = 1
2829

2930
return paths[m - 1][n - 1]
31+
32+
33+
"""
34+
공간 복잡도 최적화 풀이 - 복습 필요
35+
dp[i][j] = dp[i-1][j] + dp[i][j-1]
36+
=> dp[j] = dp[j] + dp[j-1]
37+
38+
TC: O(m * n)
39+
SC: O(n)
40+
"""
41+
42+
class Solution:
43+
def uniquePaths(self, m: int, n: int) -> int:
44+
# 첫 행은 모두 1로 초기화
45+
dp = [1] * n
46+
47+
for i in range(1, m):
48+
for j in range(1, n):
49+
dp[j] = dp[j] + dp[j - 1]
50+
51+
return dp[-1]

0 commit comments

Comments
 (0)