Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9550c34

Browse files
python: add problem 63 and unittest
1 parent 56025e3 commit 9550c34

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

Python/sln_1_100/solution_61_70.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,25 @@ def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
4343
if not obstacleGrid or not obstacleGrid[0]:
4444
return 0
4545

46-
horizon_len = len(obstacleGrid) - 1
47-
vertical_len = len(obstacleGrid[0]) - 1
48-
count = [0]
46+
horizon_len = len(obstacleGrid)
47+
vertical_len = len(obstacleGrid[0])
4948

50-
def is_valid(horizon_idx, vertical_idx):
51-
if obstacleGrid[horizon_idx][vertical_idx] != 0:
52-
return
53-
if horizon_idx == horizon_len and vertical_idx == vertical_len:
54-
count[0] += 1
55-
return
56-
57-
if horizon_idx < horizon_len:
58-
is_valid(horizon_idx + 1, vertical_idx)
59-
if vertical_idx < vertical_len:
60-
is_valid(horizon_idx, vertical_idx + 1)
61-
62-
is_valid(0, 0)
63-
return count[0]
49+
obstacleMatrix = [[0 for j in range(vertical_len)] for i in range(horizon_len)]
50+
if obstacleGrid[0][0] == 1:
51+
return 0
52+
obstacleMatrix[0][0] = 1
53+
for i in range(horizon_len):
54+
for j in range(vertical_len):
55+
if i == 0 and j == 0:
56+
continue
57+
count = 0
58+
if obstacleGrid[i][j] == 0:
59+
if i:
60+
count += obstacleMatrix[i - 1][j]
61+
if j:
62+
count += obstacleMatrix[i][j - 1]
63+
obstacleMatrix[i][j] = count
64+
return obstacleMatrix[-1][-1]
6465

6566
def addBinary(self, a, b):
6667
"""

Python/sln_1_100/test_solution_61_70.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ def test_uniquePathsWithObstacles(self):
3434
[0, 0, 0]
3535
]
3636
ret = self.sln.uniquePathsWithObstacles(matrix)
37-
print(ret)
37+
self.assertEqual(ret, 2)

0 commit comments

Comments
 (0)