Skip to content

Commit 8b440bf

Browse files
committed
week06 assignments
1 parent 169b54d commit 8b440bf

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

Week06/NOTE.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
1-
学习笔记
1+
# 学习笔记
2+
3+
#### 动态规划 (dynamic programming)
4+
5+
+ 动态规划的[定义](https://en.wikipedia.org/wiki/Dynamic_programming "打开wiki定义")
6+
+ 本质: 寻找重复性 -> 计算机指令集
7+
+ "Simplifying a complicated problem by breaking it down into simpler sub-problems." (in a recursive way)
8+
+ Divide & Conquer + Optimal substructure 分治 + 最优子结构
9+
+ 关键点:
10+
+ 动态规划 和 递归或者分治 没有根本上的区别(关键看有无最优子结构)
11+
+ 共性:找到重复子问题
12+
+ 差异性: 最优子结构、中途可以淘汰次优解
13+
14+
+ 动态规划的[解题思路](https://labuladong.gitbook.io/algo/dong-tai-gui-hua-xi-lie/dong-tai-gui-hua-xiang-jie-jin-jie "打开动态规划的解题套路框架"):
15+
+ 最优子结构
16+
+ 储存中间状态
17+
+ 递推公式(状态转移方程 或者 DP方程)
18+
19+
20+
动态规划 相关题:
21+
22+
|分类|题号|
23+
|:------:|:------:|
24+
|DP, fibonacci数列|[LC 70 爬楼梯](https://leetcode-cn.com/problems/climbing-stairs/)|
25+
|DP|[LC 62 不同路径](https://leetcode-cn.com/problems/merge-two-sorted-lists/): [MIT 最短路径算法](https://www.bilibili.com/video/av53233912?from=search&seid=2847395688604491997)|
26+
|DP|[LC 63 不同路径 II](https://leetcode-cn.com/problems/unique-paths-ii/)|
27+
|DP|[LC 1143 最长公共子序列](https://leetcode-cn.com/problems/longest-common-subsequence/)|
28+
|DP|[LC 120 三角形最短路径和](https://leetcode-cn.com/problems/triangle/description/ "打开题目")|
29+
|DP|[LC 53 最大子序和](https://leetcode-cn.com/problems/maximum-subarray/)|
30+
|DP|[LC 152 乘积最大子数组](https://leetcode-cn.com/problems/maximum-product-subarray/description/)|
31+
|DP|[LC 322 零钱兑换](https://leetcode-cn.com/problems/coin-change/description/)|
32+
|DP|[LC 198 打家劫舍](https://leetcode-cn.com/problems/house-robber/)|
33+
|DP|[LC 213 打家劫舍 II](https://leetcode-cn.com/problems/house-robber-ii/description/"打开题目")|
34+
|DP|[LC 121 买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/#/description"打开题目")|
35+
|DP|[LC 122 买卖股票的最佳时机 II](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/"打开题目")|
36+
|DP|[LC 123 买卖股票的最佳时机 III](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/"打开题目")|
37+
|DP|[LC 309 买卖股票的时机含冷冻期](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/"打开题目")|
38+
|DP|[LC 188 买卖股票的最佳时机 IV](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iv/"打开题目")|
39+
|DP|[LC 714 买卖股票的最佳时机含手续费](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/"打开题目")|
40+
|DP, 股票买卖问题, 题解|[买卖股票最佳时机的题解](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-l-3/"打开题解")|
41+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minPathSum(self, grid: [[int]]) -> int:
3+
for i in range(len(grid)):
4+
for j in range(len(grid[0])):
5+
if i == j == 0: continue
6+
elif i == 0: grid[i][j] = grid[i][j - 1] + grid[i][j]
7+
elif j == 0: grid[i][j] = grid[i - 1][j] + grid[i][j]
8+
else: grid[i][j] = min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j]
9+
return grid[-1][-1]
10+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution:
2+
def numDecodings(self, s: str) -> int:
3+
size = len(s)
4+
if size == 0:
5+
return 0
6+
7+
# dp[i]:以 s[i - 1] 结尾的前缀字符串的解码个数
8+
9+
# 分类讨论:
10+
# 1、s[i] != '0' 时,dp[i + 1] = dp[i]
11+
# 2、10 <= s[i - 1..i] <= 26 时,dp[i + 1] += dp[i - 1]
12+
dp = [0 for _ in range(size + 1)]
13+
14+
if s[0] == '0':
15+
return 0
16+
# 作为乘法因子,值为 1
17+
dp[0] = 1
18+
19+
if s[0] == '0':
20+
return 0
21+
dp[1] = 1
22+
23+
ord_zero = ord('0')
24+
for i in range(1, size):
25+
if s[i] != '0':
26+
dp[i + 1] = dp[i]
27+
28+
num = 10 * (ord(s[i - 1]) - ord_zero) + (ord(s[i]) - ord_zero)
29+
if 10 <= num <= 26:
30+
dp[i + 1] += dp[i - 1]
31+
return dp[size]

0 commit comments

Comments
 (0)