-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (37 loc) · 959 Bytes
/
index.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
39
40
41
42
43
44
45
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function (obstacleGrid) {
const m = obstacleGrid.length;
const n = obstacleGrid[0].length;
if (obstacleGrid[0][0] === 1) {
return 0;
}
const dp = obstacleGrid;
dp[0][0] = 1;
// 첫번째 열 초기화
for (let i = 1; i < m; i++) {
dp[i][0] = dp[i - 1][0] === 1 && dp[i][0] === 0 ? 1 : 0
}
// 첫번째 행 초기화
for (let j = 1; j < n; j++) {
dp[0][j] = dp[0][j - 1] === 1 && dp[0][j] === 0 ? 1 : 0
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
if (dp[i][j] === 0) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
} else {
dp[i][j] = 0
}
}
}
return dp[m - 1][n - 1];
};
const grid = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
];
console.log(uniquePathsWithObstacles(grid));