-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0063-unique-paths-ii.js
More file actions
41 lines (37 loc) · 1.1 KB
/
0063-unique-paths-ii.js
File metadata and controls
41 lines (37 loc) · 1.1 KB
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
/**
* Unique Paths II
* Time Complexity: O(rows * cols)
* Space Complexity: O(cols)
*/
var uniquePathsWithObstacles = function (grid) {
const totalRows = grid.length;
const totalColumns = grid[0].length;
if (grid[0][0] === 1) {
return 0;
}
const pathCounts = new Array(totalColumns).fill(0);
pathCounts[0] = 1;
for (let currentGridRow = 0; currentGridRow < totalRows; currentGridRow++) {
for (
let currentGridColumn = 0;
currentGridColumn < totalColumns;
currentGridColumn++
) {
if (grid[currentGridRow][currentGridColumn] === 1) {
pathCounts[currentGridColumn] = 0;
} else {
if (currentGridRow === 0 && currentGridColumn === 0) {
continue;
} else if (currentGridRow === 0) {
pathCounts[currentGridColumn] = pathCounts[currentGridColumn - 1];
} else if (currentGridColumn === 0) {
continue;
} else {
pathCounts[currentGridColumn] =
pathCounts[currentGridColumn] + pathCounts[currentGridColumn - 1];
}
}
}
}
return pathCounts[totalColumns - 1];
};