forked from Adespinoza/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_062.js
38 lines (31 loc) Β· 994 Bytes
/
problem_062.js
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
// https://leetcode.com/problems/unique-paths/description/
// Algorithmic Paradigm: Dynamic Programming
// O(NM) Time complexity
// O(NM) Space complexity
/**
* Count number of paths using right & down
* @param {number} N
* @param {number} M
* @return {number}
*/
function findPathCount(n, m) {
if (n <= 0 || m <= 0) return 0;
if (n === 1 || m === 1) return 1;
const filledArray = new Array(n).fill(0).map(() => Array(m).fill(0));
for (let r = 0; r < filledArray.length; r++) {
filledArray[0][r] = 1;
}
for (let c = 0; c < filledArray[0].length; c++) {
filledArray[c][0] = 1;
}
for (let r = 1; r < filledArray.length; r++) {
for (let c = 1; c < filledArray[r].length; c++) {
filledArray[r][c] = filledArray[r - 1][c] + filledArray[r][c - 1];
}
}
return filledArray[n - 1][m - 1];
}
console.log(findPathCount(10, 10)); // 48620
console.log(findPathCount(5, 5)); // 70
console.log(findPathCount(2, 2)); // 2
console.log(findPathCount(1, 2)); // 1