Skip to content

Commit acca70d

Browse files

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

solutions/unique-paths-ii.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
63. Unique Paths II
3+
- https://leetcode.com/problems/unique-paths-ii/
4+
- https://leetcode.com/explore/featured/card/april-leetcoding-challenge-2021/596/week-4-april-22nd-april-28th/3723/
5+
"""
6+
7+
# Method 1
8+
# DP
9+
10+
class Solution(object):
11+
def uniquePathsWithObstacles(self, obstacleGrid):
12+
"""
13+
:type obstacleGrid: List[List[int]]
14+
:rtype: int
15+
"""
16+
row = len(obstacleGrid)
17+
col = len(obstacleGrid[0])
18+
19+
if obstacleGrid[0][0] == 1:
20+
return 0
21+
22+
obstacleGrid[0][0] = 1
23+
24+
for i in range(1, col):
25+
if obstacleGrid[0][i] == 1:
26+
obstacleGrid[0][i] = 0
27+
else:
28+
obstacleGrid[0][i] = obstacleGrid[0][i - 1]
29+
30+
for j in range(1, row):
31+
if obstacleGrid[j][0] == 1:
32+
obstacleGrid[j][0] = 0
33+
else:
34+
obstacleGrid[j][0] = obstacleGrid[j - 1][0]
35+
36+
37+
for i in range(1, row):
38+
for j in range(1, col):
39+
if obstacleGrid[i][j] == 1:
40+
obstacleGrid[i][j] = 0
41+
else:
42+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
43+
44+
return obstacleGrid[row - 1][col - 1]
45+

0 commit comments

Comments
 (0)