Skip to content

Commit 0c8f914

Browse files
committed
dp 64
1 parent a520569 commit 0c8f914

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,9 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
107107
| # | Title | Solution | Time | Space | Difficulty | Note|
108108
|-----|-------| -------- | ---- | ------|------------|-----|
109109
|70|[Climbing Stairs](https://leetcode.com/problems/climbing-stairs/#/solutions)| [Python [Yu]](./dp/70.py) | _O(N)_| _O(1)_ | Easy| |
110+
111+
112+
## Dynamic Programming Medium
113+
| # | Title | Solution | Time | Space | Difficulty | Note|
114+
|-----|-------| -------- | ---- | ------|------------|-----|
115+
|64|[Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/#/submissions/1)| [Python [Yu]](./dp/64.py) | _O(N^2)_| _O(M*N)_ | Easy| [公瑾讲解](https://youtu.be/bEcW6QqgXvM)|

Diff for: dp/64.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution(object):
2+
def minPathSum(self, grid):
3+
"""
4+
:type grid: List[List[int]]
5+
:rtype: int
6+
"""
7+
#Edge
8+
if not grid:
9+
return
10+
11+
#Grid = [row][col]
12+
r = len(grid)
13+
c = len(grid[0])
14+
15+
#Create dp matrix
16+
dp = [[0 for _ in xrange(c)] for _ in xrange(r)]
17+
18+
dp[0][0] = grid[0][0]
19+
20+
for i in xrange(1, r):
21+
dp[i][0] = dp[i-1][0] + grid[i][0]
22+
23+
for i in xrange(1, c):
24+
dp[0][i] = dp[0][i-1] + grid[0][i]
25+
26+
# The rest:
27+
for i in xrange(1, r):
28+
for j in xrange(1, c):
29+
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j]
30+
31+
return dp[-1][-1]
32+
33+

Diff for: dp/show.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Grid | | | | |
2+
-----|-------| -------- | ---- |
3+
| 1|2|3|4
4+
|5|6|7|8
5+
|10|1|3|9
6+
|100|7|5|6
7+
8+
9+
DP | | | | |
10+
-----|-------| -------- | ---- |
11+
|1|3|6|10
12+
|6|9|13|18
13+
|16|10|13|24
14+
|116|17|18|24

Diff for: try.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
lst = [[0] * 3 for _ in xrange(2)]
2-
ret = ', '.join(str(x) for x in lst)
3-
print (str(lst))
4-
print ret
1+
n = 5
2+
arr = []
3+
for i in range(n):
4+
print 1
5+
for j in range(i):
6+
print 5

0 commit comments

Comments
 (0)