|
2 | 2 | Constraints:
|
3 | 3 | - 1 <= m, n <= 100
|
4 | 4 |
|
5 |
| -<Solution 1> |
| 5 | +<Solution 1: ์กฐํฉ ํ์ฉ> |
6 | 6 |
|
7 | 7 | Time Complexity: O(1)
|
8 | 8 | - math.comb() ์ฌ์ฉ
|
|
15 | 15 | 1. (m-1)๋ฒ ์๋๋ก, (n-1)๋ฒ ์ค๋ฅธ์ชฝ์ผ๋ก ๊ฐ์ผํจ -> ์ด (m+n-2)๋ฒ ์ด๋
|
16 | 16 | 2. ๊ฒฐ๊ตญ (m+n-2)๋ฒ์ ์ด๋ ์ค (n-1)๋ฒ์ ์ค๋ฅธ์ชฝ ์ด๋์ ์ ํํ๋ ์กฐํฉ์ ์
|
17 | 17 | 3. Combination ๊ณต์ ์ ์ฉ: (m+n-2)C(n-1)
|
18 |
| -
|
19 |
| -Further Consideration: |
20 |
| -- DP๋ก ํ์ด๋ณด๊ธฐ |
| 18 | + - C(m+n-2, n-1) = (m+n-2)! / ((n-1)! ร (m-1)!) |
| 19 | +- ํ์ด๊ฐ ๊ฐ๊ฒฐํจ, ํ์ง๋ง ํฐ ์์์ ์ค๋ฒํ๋ก์ฐ ๊ฐ๋ฅ์ฑ ์์ |
21 | 20 | """
|
22 | 21 | class Solution:
|
23 | 22 | def uniquePaths(self, m: int, n: int) -> int:
|
24 | 23 | from math import comb
|
25 | 24 | return comb(m+n-2, n-1)
|
26 | 25 |
|
27 | 26 | """
|
28 |
| -<Solution 2> |
| 27 | +<Solution 2: DP ํ์ฉ> |
29 | 28 |
|
30 |
| -Time Complexity: |
31 |
| -- |
| 29 | +Time Complexity: O(m * n) |
| 30 | +- m์ row, n์ column์ ๊ธธ์ด |
| 31 | +- ๋ฉ๋ชจ์ด์ ์ด์
ํ์ฉ, ๋ชจ๋ ์ขํ์์ ์ฌ๊ท ํจ์์ ํธ์ถ์ด ๋ฑ ํ๋ฒ๋ง ์ผ์ด๋๊ธฐ ๋๋ฌธ |
32 | 32 |
|
33 |
| -Space Complexity: |
34 |
| -- |
| 33 | +Space Complexity: O(m * n) |
| 34 | +- ํจ์์ ํธ์ถ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋๋ฐ ๊ฒฉ์์ ๋์ด์ ๋น๋กํ๋ ๊ณต๊ฐ์ด ํ์ |
35 | 35 |
|
36 | 36 | ํ์ด๋ฐฉ๋ฒ:
|
| 37 | +- ๊ตฌํ์ด ๋ณต์กํจ, ํ์ง๋ง ํฐ ์์์๋ ์์ ์ ์ |
| 38 | +- ์ฌ๊ท์ ๋ฉ๋ชจ์ด์ ์ด์
์ ํ์ฉํ Top-down DP ์ ๊ทผ๋ฒ |
| 39 | +- ํ์ฌ ์์น์์ ๋ชฉ์ ์ง๊น์ง์ ๊ฒฝ๋ก ์ = ์๋๋ก ์ด๋ + ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ |
| 40 | +
|
| 41 | +2x3 ๊ทธ๋ฆฌ๋ ์์: |
| 42 | +๊ฐ ์์น์์ ๋ชฉ์ ์ง๊น์ง ๊ฐ๋ ๊ฒฝ๋ก ์: |
| 43 | +(0,0)=3 (0,1)=2 (0,2)=1 |
| 44 | +(1,0)=1 (1,1)=1 (1,2)=1 |
| 45 | +
|
| 46 | +์์น (0,0)์์ ์์: |
| 47 | +dfs(0,0) = dfs(1,0) + dfs(0,1) = 1 + 2 = 3 |
| 48 | +
|
| 49 | +๊ฒฝ๋ก 3๊ฐ: |
| 50 | +1. ์ค๋ฅธ์ชฝ, ์ค๋ฅธ์ชฝ, ์๋ |
| 51 | +2. ์ค๋ฅธ์ชฝ, ์๋, ์ค๋ฅธ์ชฝ |
| 52 | +3. ์๋, ์ค๋ฅธ์ชฝ, ์ค๋ฅธ์ชฝ |
37 | 53 | """
|
| 54 | +from functools import cache |
| 55 | +class Solution: |
| 56 | + def uniquePaths(self, m: int, n: int) -> int: |
| 57 | + @cache |
| 58 | + def dfs(row, col): |
| 59 | + if row == m - 1 and col == n - 1: |
| 60 | + return 1 |
| 61 | + |
| 62 | + total = 0 |
| 63 | + |
| 64 | + if row + 1 < m: |
| 65 | + total += dfs(row + 1, col) |
| 66 | + |
| 67 | + if col + 1 < n: |
| 68 | + total += dfs(row, col + 1) |
| 69 | + |
| 70 | + return total |
38 | 71 |
|
| 72 | + return dfs(0, 0) |
0 commit comments