Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] main from itcharge:main #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 18 commits into AlgorithmAndLeetCode:main from itcharge:main
Mar 22, 2023
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
460d75d
Update Pack-CompletePack.py
itcharge Mar 21, 2023
1ae40b1
Create Pack-MultiplePack.py
itcharge Mar 21, 2023
3d4e8ce
Update 02.Knapsack-Problem-02.md
itcharge Mar 21, 2023
266cfd3
Update 03.Knapsack-Problem-03.md
itcharge Mar 21, 2023
3f1e5f4
Update Pack-CompletePack.py
itcharge Mar 21, 2023
34ab7ce
Update Pack-MultiplePack.py
itcharge Mar 21, 2023
f3dcb25
Update Pack-ZeroOnePack.py
itcharge Mar 21, 2023
24720ec
Update 01.Knapsack-Problem-01.md
itcharge Mar 21, 2023
02afef9
Update 02.Knapsack-Problem-02.md
itcharge Mar 21, 2023
9e97a03
Update 03.Knapsack-Problem-03.md
itcharge Mar 21, 2023
9142635
Update Pack-CompletePack.py
itcharge Mar 22, 2023
26a4961
Update Pack-MultiplePack.py
itcharge Mar 22, 2023
65a6ae5
Update Pack-ZeroOnePack.py
itcharge Mar 22, 2023
41eafdb
Update 01.Knapsack-Problem-01.md
itcharge Mar 22, 2023
de5b1d2
Update 02.Knapsack-Problem-02.md
itcharge Mar 22, 2023
7d6a654
Update 03.Knapsack-Problem-03.md
itcharge Mar 22, 2023
f8b4487
Update 04.Knapsack-Problem-04.md
itcharge Mar 22, 2023
7d44e7f
Update 03.Knapsack-Problem-03.md
itcharge Mar 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update 02.Knapsack-Problem-02.md
  • Loading branch information
itcharge committed Mar 21, 2023
commit 3d4e8ce679531cf2bc069738a94978fe12f44ba2
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

> **完全背包问题的特点**:每种物品有无限件。

我们可以参考「0-1 背包问题」的状态定义和基本思路。
我们可以参考「0-1 背包问题」的状态定义和基本思路,对于容量为 $w$ 的背包,最多可以装 $\frac{w}{weight[i - 1]}$ 件第 $i - 1$ 件物品。那么我们可以多加一层循环,枚举第 $i - 1$ 件物品可以选择的件数(0ドル \sim \frac{w}{weight[i - 1]}$),从而将「完全背包问题」转换为「0-1 背包问题」

#### 思路 1:动态规划

Expand Down Expand Up @@ -61,8 +61,8 @@ class Solution:
for w in range(1, W + 1):
# 枚举第 i 种物品能取个数
for k in range(w // weight[i - 1] + 1):
# dp[i][w] 取所有 dp[i][w - k * weight[i - 1] + k * value[i - 1] 中最大值
dp[i][w] = max(dp[i][w], dp[i][w - k * weight[i - 1]] + k * value[i - 1])
# dp[i][w] 取所有 dp[i - 1][w - k * weight[i - 1] + k * value[i - 1] 中最大值
dp[i][w] = max(dp[i][w], dp[i - 1][w - k * weight[i - 1]] + k * value[i - 1])

return dp[size][W]
```
Expand All @@ -72,7 +72,7 @@ class Solution:
- **时间复杂度**:$O(n \times W \times \sum\frac{W}{weight[i]}),ドル其中 $n$ 为物品种类数量,$W$ 为背包的载重上限,$weight[i]$ 是第 $i$ 种物品的重量。
- **空间复杂度**:$O(n \times W)$。

### 3.2 完全背包问题简单优化
### 3.2 完全背包问题状态转移方程优化

上之前的思路中,对于每种物品而言,每次我们都需要枚举所有可行的物品数目 $k,ドル这就大大增加了时间复杂度。

Expand Down Expand Up @@ -107,7 +107,14 @@ $(3) \quad dp[i][w] = max \lbrace dp[i - 1][w], \quad dp[i][w - weight[i - 1]] +

$\quad dp[i][w] = \begin{cases} dp[i - 1][w] & w < weight[i - 1] \cr max \lbrace dp[i - 1][w], \quad dp[i][w - weight[i - 1]] + value[i - 1] \rbrace & w \ge weight[i - 1] \end{cases}$

#### 思路 2:动态规划 + 简单优化
从上述状态转移方程我们可以看出:该式子与 0-1 背包问题中「思路 1」的状态转移式极其相似。

> 唯一区别点在于:
>
> 1. 0-1 背包问题中状态为 $dp[i - 1][w - weight[i - 1]] + value[i - 1],ドル这是第 $i - 1$ 阶段上的状态值。
> 2. 完全背包问题中状态为 $dp[i][w - weight[i - 1]] + value[i - 1],ドル这是第 $i$ 阶段上的状态值。

#### 思路 2:动态规划 + 状态转移方程优化

###### 1. 划分阶段

Expand Down Expand Up @@ -170,13 +177,6 @@ $dp[i][w] = \begin{cases} dp[i - 1][w] & w < weight[i - 1] \cr max \lbrace dp[i

所以我们没必要保存所有阶段的状态,只需要使用一个一维数组 $dp[w]$ 保存上一阶段的所有状态,采用使用「滚动数组」的方式对空间进行优化(去掉动态规划状态的第一维)。

从上述状态转移方程我们还可以看出:该式子与 0-1 背包问题中「思路 1」的状态转移式极其相似。

> 唯一区别点在于:
>
> 1. 0-1 背包问题中状态为 $dp[i - 1][w - weight[i - 1]] + value[i - 1],ドル这是第 $i - 1$ 阶段上的状态值。
> 2. 完全背包问题中状态为 $dp[i][w - weight[i - 1]] + value[i - 1],ドル这是第 $i$ 阶段上的状态值。

#### 思路 3:动态规划 + 滚动数组优化

###### 1. 划分阶段
Expand Down

AltStyle によって変換されたページ (->オリジナル) /