Skip to content

Commit f46290a

Browse files
authored
[ PS ] : Unique Paths
1 parent a739c2e commit f46290a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

unique-paths/uraflower.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* (0,0)에서 (m,n)에 도달할 수 있는 방법의 수를 반환하는 함수
3+
* @param {number} m
4+
* @param {number} n
5+
* @return {number}
6+
*/
7+
const uniquePaths = function (m, n) {
8+
const grid = Array.from({length: m}, () => Array(n).fill(0));
9+
let r = 0;
10+
let c = 0;
11+
const queue = [[r, c]];
12+
grid[r][c] = 1;
13+
14+
while (queue.length) {
15+
const [x, y] = queue.shift();
16+
17+
if (x === m - 1 && y === n - 1) {
18+
continue;
19+
}
20+
21+
if (0 <= x + 1 && x + 1 < m) {
22+
if (grid[x+1][y] === 0) queue.push([x + 1, y]);
23+
grid[x+1][y] += grid[x][y];
24+
}
25+
26+
if (0 <= y + 1 && y + 1 < n) {
27+
if (grid[x][y+1] === 0) queue.push([x, y + 1]);
28+
grid[x][y+1] += grid[x][y];
29+
}
30+
}
31+
32+
return grid[m-1][n-1];
33+
};
34+
35+
// 시간복잡도: O(m * n)
36+
// 공간복잡도: O(m * n)

0 commit comments

Comments
 (0)