Skip to content

Commit

Permalink
update dp
Browse files Browse the repository at this point in the history
  • Loading branch information
tiechui1994 committed Jun 23, 2024
1 parent 760335d commit d67bf62
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion algorithm/dp/dp.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ f[i][j] = (
```
Minimum Path Sum
设状态为 f[i][j], 表示从起点 (0; 0) 到达 (i; j ) 的最小路径和, 则状态转移方程为:
设状态为 f[i][j], 表示从起点 (0, 0) 到达 (i, j) 的最小路径和, 则状态转移方程为:
f[i][j]=min(f[i-1][j], f[i][j-1])+grid[i][j]
```
Expand Down Expand Up @@ -158,3 +158,48 @@ dict = ["leet", "code"].
dp[i] 表示源串的前i个字符可以满足分割, 那么 dp[ j ] 满足分割的条件是存在k 使得 dp [k] && substr[k,j]在字典里.
```

11.背包问题

- 01背包问题: 每种物品只有一个, 可以选择放或不放。

```
01背包问题:
dp[i][w] 表示选择 nums[0:i] 个元素, 重量不超过 w 的最大价值:
dp[i][w] = max(dp[i-1][w - nums[i]]+nums[i], dp[i-1][w])
for i in [1..N]:
for w in [1..W]:
dp[i][w] = max(
把物品 i 装进背包,
不把物品 i 装进背包
)
return dp[N][W]
```

- 完全背包问题: 每种物品有无限个, 可以选择放任意个.

```
完全背包问题:
dp[i][w] 表示选择 nums[0:i] 个元素, 重量不超过 w 的最大价值:
dp[i][w] = max(dp[i-1][w - k*nums[i]] + k*nums[i]) 0 <= k <= +OO
||、
dp[i][w] = max(dp[i-1][w - nums[i]] + nums[i])
for i in [1..N]:
for w in [1..W]:
dp[i][w] = max(
把物品 i 装进背包,
不把物品 i 装进背包
)
return dp[N][W]
```


- 多重背包问题: 每种物品有有限个, 可以选择放任意个但不能超过给定的数量.

0 comments on commit d67bf62

Please sign in to comment.