Skip to content

Commit e7f8a28

Browse files
committed
feat: 62. Unique Paths
1 parent c64fe49 commit e7f8a28

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

unique-paths/gwbaik9717.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// m: height of grid, n: width of grid
2+
// Time complexity: O(m*n)
3+
// Space complexity: O(m*n)
4+
5+
/**
6+
* @param {number} m
7+
* @param {number} n
8+
* @return {number}
9+
*/
10+
var uniquePaths = function (m, n) {
11+
const dp = Array.from({ length: m }, () =>
12+
Array.from({ length: n }, () => 0)
13+
);
14+
15+
dp[0][0] = 1;
16+
17+
for (let i = 0; i < m; i++) {
18+
for (let j = 0; j < n; j++) {
19+
if (i >= 1) {
20+
dp[i][j] += dp[i - 1][j];
21+
}
22+
23+
if (j >= 1) {
24+
dp[i][j] += dp[i][j - 1];
25+
}
26+
}
27+
}
28+
29+
return dp.at(-1).at(-1);
30+
};

0 commit comments

Comments
 (0)