Skip to content

Commit 4755985

Browse files
committed
4. Unique Paths
1 parent f8fa89a commit 4755985

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

unique-paths/sunjae95.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @description
3+
* brainstorming:
4+
* 1. dfs -> time limited
5+
* 2. dynamic programming
6+
*
7+
* time complexity: O(m * n)
8+
* space complexity: O(m * n)
9+
*/
10+
11+
var uniquePaths = function (m, n) {
12+
// initialize
13+
const dp = Array.from({ length: m }, (_, i) =>
14+
Array.from({ length: n }, (_, j) => (i === 0 || j === 0 ? 1 : 0))
15+
);
16+
17+
for (let i = 1; i < m; i++) {
18+
for (let j = 1; j < n; j++) {
19+
// recurrence relation
20+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
21+
}
22+
}
23+
24+
return dp[m - 1][n - 1];
25+
};

0 commit comments

Comments
 (0)