File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change
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 ]
You canโt perform that action at this time.
0 commit comments