We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0069e19 commit 060ae77Copy full SHA for 060ae77
unique-paths/bky373.java
@@ -1,5 +1,8 @@
1
-// time: O(m*n)
2
-// space: O(m*n)
+/*
+ [Approch 1]
3
+ - time: O(m*n)
4
+ - space: O(m*n)
5
+ */
6
class Solution {
7
8
public int uniquePaths(int m, int n) {
@@ -18,3 +21,27 @@ public int uniquePaths(int m, int n) {
18
21
return dp[m - 1][n - 1];
19
22
}
20
23
24
+
25
26
+ [Approch 2]
27
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