We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 02c4246 commit 233b460Copy full SHA for 233b460
unique-paths/forest000014.java
@@ -1,21 +1,23 @@
1
/*
2
Time Complexity: O(m * n)
3
-Space Complexity: O(m * n)
+Space Complexity: O(n)
4
*/
5
class Solution {
6
public int uniquePaths(int m, int n) {
7
- int[][] dp = new int[m][n];
+ int[] dp = new int[n];
8
9
for (int i = 0; i < n; i++) {
10
- dp[0][i] = 1;
+ dp[i] = 1;
11
}
12
+
13
for (int i = 1; i < m; i++) {
- dp[i][0] = 1;
14
+ int prev = dp[0];
15
for (int j = 1; j < n; j++) {
- dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
16
+ dp[j] += prev;
17
+ prev = dp[j];
18
19
20
- return dp[m - 1][n - 1];
21
+ return dp[n - 1];
22
23
0 commit comments