Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9064ac3

Browse files
authored
Update Unique Paths.java
1 parent 0cf4bfc commit 9064ac3

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

Medium/Unique Paths.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
class Solution {
2-
Integer[][] dp;
32
public int uniquePaths(int m, int n) {
4-
dp = new Integer[m][n];
5-
return dfs(m, n, 0, 0);
3+
Integer[][] dp = new Integer[m][n];
4+
return helper(0, 0, m, n, dp);
65
}
76

8-
private int dfs(int m, int n, int x, int y) {
9-
if (x >= m || y >= n) {
10-
return 0;
7+
private int helper(int currX, int currY, int m, int n, Integer[][] dp) {
8+
if (currX == m - 1 && currY == n - 1) {
9+
return 1;
1110
}
12-
if (dp[x][y] != null) {
13-
return dp[x][y];
11+
if (currX >= m || currY >= n) {
12+
return 0;
1413
}
15-
if (x == m - 1 && y == n - 1) {
16-
return 1;
14+
if (dp[currX][currY] != null) {
15+
return dp[currX][currY];
1716
}
18-
dp[x][y] = dfs(m, n, x + 1, y) + dfs(m, n, x, y + 1);
19-
return dp[x][y];
17+
dp[currX][currY] = helper(currX + 1, currY, m, n, dp) + helper(currX, currY + 1, m, n, dp);
18+
return dp[currX][currY];
2019
}
2120
}

0 commit comments

Comments
 (0)