Skip to content

Commit 95584fe

Browse files
committed
[7th batch] week 7 - unique paths
1 parent bd9abe6 commit 95584fe

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# 7๊ธฐ ํ’€์ด
2+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O(m * n)
3+
# - m๊ณผ n ๋งŒํผ์˜ ์ด์ค‘ ๋ฃจํ”„๋ฅผ ๋Œ๊ฒŒ ๋จ
4+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(m)
5+
# - dp์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ์ €์žฅํ•  ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” m์˜ ๊ธธ์ด์— ์ขŒ์šฐ๋จ
6+
class Solution:
7+
def uniquePaths(self, m: int, n: int) -> int:
8+
# ์ด์ „ ์—ด์˜ ๊ฐ’๋งŒ ํ•„์š”๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1์ฐจ์› ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜๋ฉฐ in-place ์—…๋ฐ์ดํŠธ๋ฅผ ํ•˜๋ฉด ๋œ๋‹ค.
9+
dp = [1 for _ in range(m)]
10+
11+
for _ in range(n - 1):
12+
for i in range(1, m):
13+
# ํ˜„์žฌ ์นธ = ์œ„์—์„œ ์˜ค๋Š” ๊ฒฝ๋กœ ์ˆ˜(๊ฐฑ์‹  ์ „ dp[i]) + ์™ผ์ชฝ์—์„œ ์˜ค๋Š” ๊ฒฝ๋กœ ์ˆ˜(dp[i-1])
14+
dp[i] = dp[i] + dp[i - 1]
15+
16+
return dp[m - 1] # ๋ชจ๋“  ๋ฃจํ”„๋ฅผ ๋Œ๊ณ  ๋‚˜๋ฉด ๋„์ฐฉ์ง€ ๋„๋‹ฌ ๊ฐ€๋Šฅ ๋ฐฉ๋ฒ•์ˆ˜๊ฐ€ ์ €์žฅ์ด ๋˜์–ด ์žˆ๋‹ค.

0 commit comments

Comments
ย (0)