Skip to content

Commit c3251f9

Browse files
authored
Update Unique Paths.java
1 parent be9503e commit c3251f9

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

Medium/Unique Paths.java

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
class Solution {
22
public int uniquePaths(int m, int n) {
3-
Integer[][] dp = new Integer[m][n];
4-
return helper(0, 0, m, n, dp);
5-
}
6-
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;
10-
}
11-
if (currX >= m || currY >= n) {
12-
return 0;
3+
int[][] dp = new int[m][n];
4+
for (int[] arr : dp) {
5+
Arrays.fill(arr, 1);
136
}
14-
if (dp[currX][currY] != null) {
15-
return dp[currX][currY];
7+
for (int i = 1; i < m; i++) {
8+
for (int j = 1; j < n; j++) {
9+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
10+
}
1611
}
17-
dp[currX][currY] = helper(currX + 1, currY, m, n, dp) + helper(currX, currY + 1, m, n, dp);
18-
return dp[currX][currY];
12+
return dp[m - 1][n - 1];
1913
}
2014
}

0 commit comments

Comments
 (0)