forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbky373.java
More file actions
47 lines (38 loc) · 879 Bytes
/
bky373.java
File metadata and controls
47 lines (38 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
[Approch 1]
- time: O(m*n)
- space: O(m*n)
*/
class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n];
for (int[] row : dp) {
Arrays.fill(row, 1);
}
for (int row = 1; row < m; row++) {
for (int col = 1; col < n; col++) {
dp[row][col] = dp[row - 1][col] + dp[row][col - 1];
}
}
return dp[m - 1][n - 1];
}
}
/*
[Approch 2]
- time: O(m*n)
- space: O(n)
*/
class Solution {
public int uniquePaths(int m, int n) {
int[] upper = new int[n];
Arrays.fill(upper, 1);
for (int i = 1; i < m; i++) {
int left = 1;
for (int j = 1; j < n; j++) {
left += upper[j];
upper[j] = left;
}
}
return upper[n - 1];
}
}