Skip to content

Commit bde92ae

Browse files
committed
feat: [Week 07-4] solve Unique Paths
1 parent 2c79e10 commit bde92ae

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

โ€Žunique-paths/Chaedie.pyโ€Ž

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
1 0 0 0 0 0 0
3+
0 0 0 0 0 0 0
4+
0 0 0 0 0 0 0
5+
6+
Solution:
7+
1) grid์—์„œ 0,0์„ ์ œ์™ธํ•˜๊ณค ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด left way + upper way ๊ฐฏ์ˆ˜๋ฐ–์— ์—†๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
8+
2) ๋”ฐ๋ผ์„œ grid๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ grid index์˜ ๊ฒฝ๊ณ„๋ฅผ ๋„˜์–ด๊ฐ€์ง€ ์•Š์„ ๊ฒฝ์šฐ left way + upper way ๊ฐฏ์ˆ˜๋ฅผ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.
9+
3) ๋งˆ์ง€๋ง‰ grid์˜ ์šฐํ•˜๋‹จ์˜ ๊ฐ’์„ return ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
10+
Time: O(mn) (์›์†Œ์˜ ๊ฐฏ์ˆ˜)
11+
Space: O(mn) (์›์†Œ์˜ ๊ฐฏ์ˆ˜)
12+
"""
13+
14+
15+
class Solution:
16+
def uniquePaths(self, m: int, n: int) -> int:
17+
dp = []
18+
for i in range(m):
19+
dp.append([0] * n)
20+
print(dp)
21+
22+
for i in range(m):
23+
for j in range(n):
24+
if i == 0 and j == 0:
25+
dp[i][j] = 1
26+
continue
27+
28+
up_value = 0 if i - 1 < 0 or i - 1 >= m else dp[i - 1][j]
29+
left_value = 0 if j - 1 < 0 or j - 1 >= n else dp[i][j - 1]
30+
31+
dp[i][j] = up_value + left_value
32+
33+
return dp[m - 1][n - 1]

0 commit comments

Comments
ย (0)