File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * <a href="https://leetcode.com/problems/unique-paths/">week07-4. unique-paths</a>
3+ * <li>Description: return the number of possible unique paths to reach the bottom-right corner</li>
4+ * <li>Topics: Math, Dynamic Programming, Combinatorics </li>
5+ * <li>Time Complexity: O(M×N), Runtime 2ms </li>
6+ * <li>Space Complexity: O(M×N), Memory 40.76MB </li>
7+ */
8+ class Solution {
9+ public int uniquePaths (int m , int n ) {
10+ int [][] dirs = {{1 , 0 }, {0 , 1 }};
11+ int [][] dp = new int [m ][n ];
12+ dp [0 ][0 ] = 1 ;
13+
14+ Queue <int []> queue = new LinkedList <>();
15+ queue .add (new int []{0 , 0 });
16+
17+ while (!queue .isEmpty ()) {
18+ int [] cur = queue .poll ();
19+ int cr = cur [0 ];
20+ int cc = cur [1 ];
21+
22+ for (int i = 0 ; i < 2 ; i ++) {
23+ int nr = cr + dirs [i ][0 ];
24+ int nc = cc + dirs [i ][1 ];
25+
26+ if (nr > m - 1 || nc > n - 1 ) {
27+ continue ;
28+ }
29+
30+ if (dp [nr ][nc ] == 0 ) {
31+ queue .add (new int []{nr , nc });
32+ }
33+ dp [nr ][nc ] += dp [cr ][cc ];
34+ }
35+ }
36+
37+ return dp [m - 1 ][n - 1 ];
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments