-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique-paths-ii.js
More file actions
49 lines (40 loc) · 1.06 KB
/
unique-paths-ii.js
File metadata and controls
49 lines (40 loc) · 1.06 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
42
43
44
45
46
47
48
49
/**
* Problem: Unique Paths II
* Link: https://leetcode.com/problems/unique-paths-ii/
* Difficulty: Medium
*
* Same as Unique Paths but with obstacles (1 = obstacle).
*
* Time Complexity: O(m * n)
* Space Complexity: O(n)
*/
// JavaScript Solution - DP
function uniquePathsWithObstacles(obstacleGrid) {
const n = obstacleGrid[0].length;
const dp = new Array(n).fill(0);
dp[0] = 1; // start position
for (let r = 0; r < obstacleGrid.length; r++) {
for (let c = 0; c < n; c++) {
if (obstacleGrid[r][c] === 1) {
dp[c] = 0; // obstacle, no paths through here
} else if (c > 0) {
dp[c] += dp[c - 1]; // paths from above + from left
}
}
}
return dp[n - 1];
}
module.exports = uniquePathsWithObstacles;
/* Python Solution:
def uniquePathsWithObstacles(obstacleGrid):
n = len(obstacleGrid[0])
dp = [0] * n
dp[0] = 1
for row in obstacleGrid:
for c in range(n):
if row[c] == 1:
dp[c] = 0
elif c > 0:
dp[c] += dp[c-1]
return dp[-1]
*/