-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniquePath2.py
More file actions
45 lines (34 loc) · 1.36 KB
/
UniquePath2.py
File metadata and controls
45 lines (34 loc) · 1.36 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
#https://leetcode.com/problems/unique-paths-ii/
class Solution(object):
def uniquePathsWithObstacles(self, obstacleGrid):
"""
:type obstacleGrid: List[List[int]]
:rtype: int
"""
if not obstacleGrid:
return
rowSize = len(obstacleGrid)
colSize = len(obstacleGrid[0])
#if rowSize == 1 and colSize == 1:
# return 0
def check(row, col, grid):
return row < rowSize and col < colSize
def isValidBox(row, col, grid):
return grid[row][col] == 0
def helper(row, col, memo, grid):
if not isValidBox(row, col, grid):
return 0
if (row, col) in memo:
return memo[(row,col)]
right,down, totalWays = 0 , 0, 0
if check(row + 1, col, grid):
right = helper(row + 1, col, memo, grid)
if check(row, col + 1, grid):
down = helper(row, col + 1, memo, grid)
if row == rowSize - 1 and col == colSize - 1:
totalWays = 1
totalWays = right + down + totalWays
memo[(row, col)] = totalWays
return totalWays
memo = {}
return helper(0,0, memo, obstacleGrid)