-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path063.js
More file actions
31 lines (28 loc) · 824 Bytes
/
063.js
File metadata and controls
31 lines (28 loc) · 824 Bytes
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
/**
* @param {number[][]} obstacleGrid
* @return {number}
*/
var uniquePathsWithObstacles = function(obstacleGrid) {
let map = {};
const rowCount = obstacleGrid.length;
if (rowCount === 0) return 0;
const colCount = obstacleGrid[0].length;
if (colCount === 0) return 0;
for (let row = 0; row < rowCount; ++row) {
for (let col = 0; col < colCount; ++col) {
if (obstacleGrid[row][col] === 1) {
map[row * colCount + col] = 0;
continue;
}
let pathCount = 0;
if (row === 0 && col === 0) {
pathCount = 1;
} else {
if (row !== 0) pathCount += map[(row - 1) * colCount + col];
if (col !== 0) pathCount += map[row * colCount + col - 1];
}
map[row * colCount + col] = pathCount;
}
}
return map[rowCount * colCount - 1];
};