-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniquePathsWithObstacles.py
More file actions
38 lines (34 loc) · 1.1 KB
/
Copy pathuniquePathsWithObstacles.py
File metadata and controls
38 lines (34 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
# coding=utf-8
"""
@project : algorithmPython
@ide : PyCharm
@file : uniquePathsWithObstacles
@author : illusion
@desc : 63. 不同路径 II https://leetcode-cn.com/problems/unique-paths-ii/
@create : 2021/6/8 1:36 下午:40
"""
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: [[int]]) -> int:
if not len(obstacleGrid) or not len(obstacleGrid[0]):
return 0
if obstacleGrid[-1][-1] == 1:
return 0
col_length = len(obstacleGrid[0])
dp = []
has_obs = False
for col in range(col_length):
if obstacleGrid[0][col] == 1:
has_obs = True
if has_obs:
dp.append(0)
else:
dp.append(1)
for row in range(1, len(obstacleGrid)):
for col in range(col_length):
if obstacleGrid[row][col] == 1:
dp[col] = 0
elif col > 0:
dp[col] += dp[col - 1]
return dp[-1]
if __name__ == '__main__':
print(Solution().uniquePathsWithObstacles([[0, 1], [0, 0]]))