-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniquePaths.js
More file actions
50 lines (42 loc) · 1.2 KB
/
UniquePaths.js
File metadata and controls
50 lines (42 loc) · 1.2 KB
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
48
49
50
var uniquePaths = function(m, n) {
let recur = (i, j) => {
if(i>m || j > n) return 0;
if(i == m && j == n) return 1;
let fromRight = recur(i, j+1);
let fromBottom = recur(i+1, j);
return fromRight + fromBottom;
}
let dpmethod_using_2darray = (m, n) => {
let dp = new Array(m);
for(let i=0;i<m;i++) {
dp[i] = new Array(n).fill(1);
}
for(let i=1;i<m;i++) {
for(let j=1;j<n;j++) {
dp[i][j] = dp[i][j-1] + dp[i-1][j]
}
}
return dp[m-1][n-1];
}
let dpmethod_using_only2rows = (m, n) => {
let prev = new Array(n).fill(1);
let cur = [...prev];
for(let i=1;i<m;i++) {
for(let j=1;j<n;j++) {
cur[j] = cur[j-1] + prev[j]
}
prev = [...cur]
}
return cur[n-1];
}
let dpmethod = (m, n) => {
let cur = new Array(n).fill(1);
for(let i = 1; i<m;i++) {
for(let j=1; j<n;j++) {
cur[j] = cur[j-1] + cur[j];
}
}
return cur[n-1];
}
return dpmethod(m,n);
};