Skip to content

Commit 060ae77

Browse files
committed
Enhance space complexity
1 parent 0069e19 commit 060ae77

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

unique-paths/bky373.java

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
// time: O(m*n)
2-
// space: O(m*n)
1+
/*
2+
[Approch 1]
3+
- time: O(m*n)
4+
- space: O(m*n)
5+
*/
36
class Solution {
47

58
public int uniquePaths(int m, int n) {
@@ -18,3 +21,27 @@ public int uniquePaths(int m, int n) {
1821
return dp[m - 1][n - 1];
1922
}
2023
}
24+
25+
/*
26+
[Approch 2]
27+
- time: O(m*n)
28+
- space: O(n)
29+
*/
30+
class Solution {
31+
32+
public int uniquePaths(int m, int n) {
33+
int[] upper = new int[n];
34+
35+
Arrays.fill(upper, 1);
36+
37+
for (int i = 1; i < m; i++) {
38+
int left = 1;
39+
for (int j = 1; j < n; j++) {
40+
left += upper[j];
41+
upper[j] = left;
42+
}
43+
}
44+
45+
return upper[n - 1];
46+
}
47+
}

0 commit comments

Comments
 (0)