Skip to content

Commit 0fd165a

Browse files
committed
路径求和(dp经典, 暂时用二维数组存储中间状态, 可考虑滚动数组降维)
1 parent d8e35a5 commit 0fd165a

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

DynamicProgramming/122_BestTimeToBuyAndSellStockII.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,8 @@ def maxProfit(self, prices):
2929
# 搜索所有的上升子序列, 求和
3030
def dynamic_max_profit(self, prices):
3131
ans = 0
32-
start = 0
3332
for i in range(1, len(prices)):
34-
delta = prices[i] - prices[i - 1]
35-
if delta < 0:
36-
ans += prices[i - 1] - prices[start]
37-
start = i
38-
39-
if prices[-1] > prices[start]:
40-
ans += prices[-1] - prices[start]
33+
if prices[i] > prices[i - 1]:
34+
ans += prices[i] - prices[i - 1]
4135

4236
return ans

DynamicProgramming/62_UniquePaths.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# coding: utf8
2+
3+
4+
"""
5+
题目链接: https://leetcode.com/problems/unique-paths/description.
6+
题目描述:
7+
8+
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
9+
10+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right
11+
corner of the grid (marked 'Finish' in the diagram below).
12+
13+
How many possible unique paths are there?
14+
15+
16+
Above is a 3 x 7 grid. How many possible unique paths are there?
17+
18+
Note: m and n will be at most 100.
19+
20+
"""
21+
22+
23+
class Solution(object):
24+
def uniquePaths(self, m, n):
25+
"""
26+
:type m: int
27+
:type n: int
28+
:rtype: int
29+
"""
30+
if m <= 1 or n <= 1:
31+
return 1
32+
33+
return self.dynamic_unique_paths(m, n)
34+
35+
# 常规dp
36+
def dynamic_unique_paths(self, m, n):
37+
dp = [[0 for _ in range(n)] for _ in range(m)]
38+
for i in range(m):
39+
dp[i][0] = 1
40+
for j in range(n):
41+
dp[0][j] = 1
42+
for i in range(1, m):
43+
for j in range(1, n):
44+
dp[i][j] = dp[i][j - 1] + dp[i - 1][j]
45+
46+
return dp[m - 1][n - 1]
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# coding: utf8
2+
3+
4+
"""
5+
题目链接: https://leetcode.com/problems/minimum-path-sum/description.
6+
题目描述:
7+
8+
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the
9+
sum of all numbers along its path.
10+
11+
Note: You can only move either down or right at any point in time.
12+
13+
Example 1:
14+
[[1,3,1],
15+
[1,5,1],
16+
[4,2,1]]
17+
Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.
18+
19+
"""
20+
21+
22+
class Solution(object):
23+
def minPathSum(self, grid):
24+
"""
25+
:type grid: List[List[int]]
26+
:rtype: int
27+
"""
28+
if not any(grid):
29+
return 0
30+
31+
return self.dynamic_min_path_sum(grid)
32+
33+
# 动态规划:
34+
# 递推公式: dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j], i>=1, j>=1
35+
def dynamic_min_path_sum(self, grid):
36+
rows = len(grid)
37+
columns = len(grid[0])
38+
dp = [[0 for _ in range(columns)] for _ in range(rows)]
39+
dp[0][0] = grid[0][0]
40+
for i in range(1, rows):
41+
dp[i][0] = dp[i - 1][0] + grid[i][0]
42+
for j in range(1, columns):
43+
dp[0][j] = dp[0][j - 1] + grid[0][j]
44+
45+
for i in range(1, rows):
46+
for j in range(1, columns):
47+
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
48+
49+
return dp[rows - 1][columns - 1]

0 commit comments

Comments
 (0)