Skip to content

Commit 073e7e6

Browse files
april 28
1 parent f8ec28c commit 073e7e6

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

Challenges/2021/April-LeetCoding-Challenge.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ None
5252
|48.|[Rotate Image](https://leetcode.com/problems/rotate-image/)|[Python](/Medium/48.RotateImage.py)|Medium|
5353
|1642.|[Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/)|~~Python~~|Medium|
5454
|326.|[Power of Three](https://leetcode.com/problems/power-of-three/)|[Python](/Easy/326.PowerofThree.py)|Easy|
55+
|63.|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)|[Python](/Medium/63.UniquePathsII.py)|Medium|
5556

5657
## License
5758
The code is open-source and licensed under the [MIT License](/LICENSE).

Medium/63.UniquePathsII.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
A robot is located at the top-left corner of a m x n grid
3+
(marked 'Start' in the diagram below).
4+
5+
The robot can only move either down or right at any point
6+
in time. The robot is trying to reach the bottom-right
7+
corner of the grid (marked 'Finish' in the diagram below).
8+
9+
Now consider if some obstacles are added to the grids.
10+
How many unique paths would there be?
11+
12+
An obstacle and space is marked as 1 and 0 respectively
13+
in the grid.
14+
15+
Example:
16+
Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
17+
Output: 2
18+
Explanation: There is one obstacle in the middle of the
19+
3x3 grid above.
20+
There are two ways to reach the bottom-right
21+
corner:
22+
1. Right -> Right -> Down -> Down
23+
2. Down -> Down -> Right -> Right
24+
25+
Example:
26+
Input: obstacleGrid = [[0,1],[0,0]]
27+
Output: 1
28+
29+
Constraints:
30+
- m == obstacleGrid.length
31+
- n == obstacleGrid[i].length
32+
- 1 <= m, n <= 100
33+
- obstacleGrid[i][j] is 0 or 1.
34+
'''
35+
#Difficulty: Medium
36+
#41 / 41 test cases passed.
37+
#Runtime: 40 ms
38+
#Memory Usage: 14.1 MB
39+
40+
#Runtime: 40 ms, faster than 78.35% of Python3 online submissions for Unique Paths II.
41+
#Memory Usage: 14.1 MB, less than 94.59% of Python3 online submissions for Unique Paths II.
42+
43+
class Solution:
44+
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
45+
46+
# count paths in the first row
47+
for j in range(len(obstacleGrid[0])):
48+
if obstacleGrid[0][j] != 1:
49+
if j == 0:
50+
obstacleGrid[0][j] = 1
51+
else:
52+
obstacleGrid[0][j] += obstacleGrid[0][j-1]
53+
else:
54+
obstacleGrid[0][j] = 0
55+
56+
# count paths in the rest of the matrix
57+
for i in range(1, len(obstacleGrid)):
58+
for j in range(len(obstacleGrid[0])):
59+
if obstacleGrid[i][j] != 1:
60+
if j != 0:
61+
obstacleGrid[i][j] += (obstacleGrid[i][j-1] + obstacleGrid[i-1][j])
62+
else:
63+
obstacleGrid[i][j] += obstacleGrid[i-1][j]
64+
else:
65+
obstacleGrid[i][j] = 0
66+
return obstacleGrid[-1][-1]

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-548%2F1665-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-549%2F1665-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -22,7 +22,7 @@ In this repository provided my Python solutions of LeetCode problems.
2222
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
2323
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
2424
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
25-
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 20/30
25+
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 21/30
2626
<br><br>
2727
## Solutions
2828
*P.S. If you like this, please leave me a star.*
@@ -73,6 +73,7 @@ In this repository provided my Python solutions of LeetCode problems.
7373
|58.|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Python](/Easy/58.LengthofLastWord(string).py)|Easy|`String`|
7474
|59.|[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)|[Python](/Medium/59.SpiralMatrixII.py)|Medium|spaghetti|
7575
|61.|[Rotate List](https://leetcode.com/problems/rotate-list/)|[Python](/Medium/61.RotateList.py)|Medium|`Linked List`|
76+
|63.|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)|[Python](/Medium/63.UniquePathsII.py)|Medium|`DP`|
7677
|64.|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)|[Python](https://github.com/YuriSpiridonov/30-Day-LeetCoding-Challenge/blob/master/Week%203/MinimumPathSum.py)|Medium|
7778
|66.|[Plus One](https://leetcode.com/problems/plus-one/)|[Python](https://github.com/YuriSpiridonov/LeetCode/blob/master/Easy/66.PlusOne.py)|Easy||
7879
|67.|[Add Binary](https://leetcode.com/problems/add-binary/)|[Python](/Easy/67.AddBinary.py)|Easy||

0 commit comments

Comments
 (0)