Skip to content

Commit 759e2e8

Browse files
committed
leetcode 63
python
1 parent 2b547ad commit 759e2e8

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Personal Practice Set - Doing One a Day (sometimes) in a Variety of Languages (M
5555
| 56 | Medium | [Merge Intervals](leetcode/56-Medium-Merge-Intervals/problem.md) | [Python](leetcode/56-Medium-Merge-Intervals/answer.py) |
5656
| 58 | Easy | [Length of Last Word](leetcode/58-Easy-Length-Of-Last-Word/problem.md) | [Python](leetcode/58-Easy-Length-Of-Last-Word/answer.py) |
5757
| 62 | Medium | [Unique Paths](leetcode/62-Medium-Unique-Paths/problem.md) | [Python](leetcode/62-Medium-Unique-Paths/answer.py) |
58+
| 63 | Medium | [Unique Paths II](leetcode/63-Medium-Unique-Paths-II/problem.md) | [Python](leetcode/63-Medium-Unique-Paths-II/answer.py) |
5859
| 64 | Medium | [Minimum Path Sum](leetcode/64-Medium-Minimum-Path-Sum/problem.md) | [Python](leetcode/64-Medium-Minimum-Path-Sum/answer.py) |
5960
| 66 | Easy | [Plus One](leetcode/66-Easy-Plus-One/problem.md) | [Python](leetcode/66-Easy-Plus-One/answer.py) |
6061
| 67 | Easy | [Add Binary](leetcode/67-Easy-Add-Binary/problem.md) | [Python](leetcode/67-Easy-Add-Binary/answer.py) |
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/python3
2+
3+
#------------------------------------------------------------------------------
4+
5+
class Solution(object):
6+
def uniquePathsWithObstacles(self, grid):
7+
"""
8+
:type obstacleGrid: List[List[int]]
9+
:rtype: int
10+
"""
11+
if not grid or not grid[0] or grid[0][0] == 1:
12+
return 0
13+
# **NOTE** Cannot use multiplication because it copies references
14+
# Must use list comprehension for this to create cache
15+
# cache = [[0]*len(grid[0])]*len(grid)
16+
cache = [[0]*len(grid[0]) for _ in range(len(grid))]
17+
cache[0][0] = 1
18+
19+
# Set side paths
20+
for row in range(1, len(grid)):
21+
if grid[row][0] == 1:
22+
break
23+
cache[row][0] = 1
24+
for col in range(1, len(grid[0])):
25+
if grid[0][col] == 1:
26+
break
27+
cache[0][col] = 1
28+
29+
# Set rest of cache
30+
for row in range(1, len(grid)):
31+
for col in range(1, len(grid[0])):
32+
if grid[row][col] == 0:
33+
cache[row][col] = cache[row-1][col] + cache[row][col-1]
34+
35+
return cache[-1][-1]
36+
37+
#------------------------------------------------------------------------------
38+
#Testing
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Problem 63: Unique Paths II
2+
3+
#### Difficulty: Medium
4+
5+
#### Problem
6+
7+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
8+
9+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
10+
11+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
12+
13+
An obstacle and empty space is marked as 1 and 0 respectively in the grid.
14+
15+
#### Example
16+
17+
<pre>
18+
19+
Input:
20+
[
21+
[0,0,0],
22+
[0,1,0],
23+
[0,0,0]
24+
]
25+
Output: 2
26+
Explanation:
27+
There is one obstacle in the middle of the 3x3 grid above.
28+
There are two ways to reach the bottom-right corner:
29+
1. Right -> Right -> Down -> Down
30+
2. Down -> Down -> Right -> Right
31+
32+
</pre>
33+

0 commit comments

Comments
 (0)