-
Notifications
You must be signed in to change notification settings - Fork 70
/
res.js
26 lines (24 loc) · 771 Bytes
/
res.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
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
const n = obstacleGrid.length;
if (!n) return 0;
const m = obstacleGrid[0].length;
if (!m) return 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
if (i === 0 && j === 0) {
obstacleGrid[i][j] = obstacleGrid[i][j] ? 0 : 1;
} else if (i == 0) {
obstacleGrid[i][j] = obstacleGrid[i][j] || !obstacleGrid[i][j-1] ? 0 : 1;
} else if (j == 0) {
obstacleGrid[i][j] = obstacleGrid[i][j] || !obstacleGrid[i-1][j] ? 0 : 1;
} else {
obstacleGrid[i][j] = obstacleGrid[i][j] ? 0 : obstacleGrid[i-1][j] + obstacleGrid[i][j-1];
}
}
}
return obstacleGrid[n-1][m-1];
};