Skip to content

Commit f3dbfdf

Browse files
Add files via upload
1 parent 5804d5c commit f3dbfdf

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Unique Paths II/Unique_Paths_II.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# 动态规划
2+
# 36ms 99.37%
3+
class Solution:
4+
def uniquePathsWithObstacles(self, obstacleGrid):
5+
"""
6+
:type obstacleGrid: List[List[int]]
7+
:rtype: int
8+
"""
9+
m, n = len(obstacleGrid), len(obstacleGrid[0])
10+
if m is 0 or n is 0 or obstacleGrid[0][0] is 1:
11+
return 0
12+
13+
score = [[0 for j in range(n)] for i in range(m)]
14+
for i in range(1, m):
15+
if 1 in [row[0] for row in obstacleGrid[:i + 1]]:
16+
score[i][0] = 0
17+
else:
18+
score[i][0] = 1
19+
for j in range(n):
20+
if 1 in obstacleGrid[0][:j + 1]:
21+
score[0][j] = 0
22+
else:
23+
score[0][j] = 1
24+
for i in range(1, m):
25+
for j in range(1, n):
26+
if obstacleGrid[i][j] is 1:
27+
score[i][j] = 0
28+
else:
29+
score[i][j] = score[i-1][j] + score[i][j-1]
30+
return score[-1][-1]

0 commit comments

Comments
 (0)