Skip to content

Commit 0e32e92

Browse files
committed
unique-paths sol (py)
1 parent a08ff79 commit 0e32e92

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

โ€Žunique-paths/hi-rachel.pyโ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
https://leetcode.com/problems/unique-paths/description/
3+
4+
์•„๋ž˜๋กœ ์ด๋™ ํ˜น์€ (1, 0)
5+
์˜ค๋ฅธ์ชฝ ์ด๋™๋งŒ ๊ฐ€๋Šฅ (0, 1)
6+
7+
m => rows, n = cols
8+
๋กœ๋ด‡์ด (0, 0)์—์„œ (m-1, n-1)์— ๋„์ฐฉ ๊ฐ€๋Šฅํ•œ unique paths ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
9+
10+
ํ’€์ด ์‹œ๊ฐ„: 16๋ถ„
11+
์ฒ˜์Œ์— ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ•  ์ค„ ๋ชฐ๋ž์ง€๋งŒ, ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋ฉฐ ๋ˆ„์  ๊ทœ์น™์„ ์ฐพ์Œ (์œ„, ์™ผ์ชฝ ๊ฐ’ ๋”ํ•ด๋‚˜๊ฐ€๊ธฐ)
12+
13+
TC: O(m * n)
14+
SC: O(m * n)
15+
"""
16+
17+
class Solution:
18+
def uniquePaths(self, m: int, n: int) -> int:
19+
paths = [[0] * n for _ in range(m)]
20+
paths[0][0] = 1
21+
22+
for i in range(m):
23+
for j in range(n):
24+
if i - 1 >= 0 and j - 1 >= 0:
25+
paths[i][j] = paths[i - 1][j] + paths[i][j - 1]
26+
else:
27+
paths[i][j] = 1
28+
29+
return paths[m - 1][n - 1]

0 commit comments

Comments
ย (0)