Skip to content

Commit c835ceb

Browse files
committed
62_Unique_Paths
63_Unique_Paths_II 70_Climbing_Stairs
1 parent 04cb2f7 commit c835ceb

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@
2424
| 24 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/24_Swap_Nodes_in_Pairs.py) | Add a dummy and store the prev |
2525
| 28 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/28_Implement_strStr().py) | 1. O(nm) comparison <br>2. KMP |
2626
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/54_Spiral_Matrix.py) | O(nm) 1. Turn in the corner and loop<br>2. (0, 1) -> (1, 0) -> (0, -1) -> (-1, 0) |
27+
| 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/62_Unique_Paths.py) | 1. Bottom-up DP, dp[i][j] = dmap[i-1][j] + dmap[i][j-1], O(mn) and O(mn)<br>2. Combination (m+n-2, m-1) |
28+
| 63 | [Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/63_Unique_Paths_II.py) | Bottom-up DP, dp[i][j] = dmap[i-1][j] + dmap[i][j-1] (if block, then 0), O(mn) and O(mn) |
2729
| 65 | [Valid Number](https://leetcode.com/problems/valid-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/65_Valid_Number.py) | 1. strip leading and tailing space, then check float using exception, check e using split <br>2. check is number split by . or e, note that number after e may be negative |
2830
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/66_Plus_One.py) | Check if current digit == 9. |
31+
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/70_Climbing_Stairs.py) | Bottom-up DP, dp[i] = dp[i - 2] + dp[i- 1] <br>1. O(n) and O(n)<br>2. Only two variables are needed, O(n) and O(1) |
2932
| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/72_Edit_Distance.py) | [Background](https://en.wikipedia.org/wiki/Edit_distance)<br> 1. DP O(n^2) space<br>2. DP O(n) space |
3033
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/98_Validate_Binary_Search_Tree.py) | 1. Stack O(n) and O(n)<br>2. Recursion O(n) and O(n) |
3134
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/104_Maximum_Depth_of_Binary_Tree.py)| Recursion max(left, right) + 1 |

python/62_Unique_Paths.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ def uniquePaths(self, m, n):
55
:type n: int
66
:rtype: int
77
"""
8-
dmap = []
9-
for i in range(m):
10-
temp = [0] * n
11-
dmap.append(temp)
8+
dmap = [[0] * n for _ in range(m)]
129
for i in range(m):
1310
dmap[i][0] = 1
1411
for j in range(n):

python/63_Unique_Paths_II.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
class Solution(object):
2+
# def uniquePathsWithObstacles(self, obstacleGrid):
3+
# """
4+
# :type obstacleGrid: List[List[int]]
5+
# :rtype: int
6+
# """
7+
# m, n = len(obstacleGrid), len(obstacleGrid[0])
8+
# dmap = [[0] * n for _ in range(m)]
9+
# for i in range(m):
10+
# if obstacleGrid[i][0] != 1:
11+
# dmap[i][0] = 1
12+
# else:
13+
# break
14+
# for j in range(n):
15+
# if obstacleGrid[0][j] != 1:
16+
# dmap[0][j] = 1
17+
# else:
18+
# break
19+
# for i in range(1, m):
20+
# for j in range(1, n):
21+
# if obstacleGrid[i][j] == 1:
22+
# continue
23+
# l = u = 0
24+
# if i - 1 >= 0:
25+
# u = dmap[i - 1][j]
26+
# if j - 1 >= 0:
27+
# l = dmap[i][j - 1]
28+
# dmap[i][j] = l + u
29+
# return dmap[m - 1][n - 1]
30+
231
def uniquePathsWithObstacles(self, obstacleGrid):
3-
"""
4-
:type obstacleGrid: List[List[int]]
5-
:rtype: int
6-
"""
732
m, n = len(obstacleGrid), len(obstacleGrid[0])
8-
dmap = []
9-
for i in range(m):
10-
temp = [0] * n
11-
dmap.append(temp)
12-
for i in range(m):
13-
if obstacleGrid[i][0] != 1:
14-
dmap[i][0] = 1
15-
else:
16-
break
17-
for j in range(n):
18-
if obstacleGrid[0][j] != 1:
19-
dmap[0][j] = 1
20-
else:
21-
break
22-
for i in range(1, m):
23-
for j in range(1, n):
33+
if m == 0:
34+
return 0
35+
dmap = [[0] * (n + 1) for _ in range(m + 1)]
36+
dmap[m - 1][n] = 1
37+
for i in range(m - 1, -1, -1):
38+
for j in range(n - 1, -1, -1):
2439
if obstacleGrid[i][j] == 1:
25-
continue
26-
l = u = 0
27-
if i - 1 >= 0:
28-
u = dmap[i - 1][j]
29-
if j - 1 >= 0:
30-
l = dmap[i][j - 1]
31-
dmap[i][j] = l + u
32-
return dmap[m - 1][n - 1]
40+
dmap[i][j] = 0
41+
else:
42+
dmap[i][j] = dmap[i][j + 1] + dmap[i + 1][j]
43+
return dmap[0][0]

0 commit comments

Comments
 (0)