Skip to content

Commit 38f7eac

Browse files
committed
unique-paths solution
1 parent c75d8e9 commit 38f7eac

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

โ€Žunique-paths/jdy8739.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var uniquePaths = function (m, n) {
2+
const cache = new Map();
3+
4+
const dfs = (row, col) => {
5+
const cacheKey = `${row}-${col}`;
6+
7+
if (cache.has(cacheKey)) {
8+
return cache.get(cacheKey);
9+
}
10+
11+
if (row === m - 1 && col === n - 1) {
12+
return 1;
13+
}
14+
15+
let count = 0;
16+
17+
if (row < m - 1) {
18+
count += dfs(row + 1, col);
19+
}
20+
21+
if (col < n - 1) {
22+
count += dfs(row, col + 1);
23+
}
24+
25+
cache.set(cacheKey, count);
26+
27+
return count;
28+
}
29+
30+
return dfs(0, 0);
31+
};
32+
33+
// ์‹œ๊ฐ„๋ณต์žก๋„ O(m * n)
34+
// ๊ณต๊ฐ„๋ณต์žก๋„ O(m * n) - 1 (matrix[m][n]์— ๋Œ€ํ•œ ์บ์‹œ๋Š” ํฌํ•จ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ)
35+

0 commit comments

Comments
ย (0)