forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #72
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
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
74f0d9f
Update Pack-ZeroOnePack.py
itcharge 8831fa7
Update Pack-CompletePack.py
itcharge d77a20e
Update Pack-MultiplePack.py
itcharge 9ce73dd
Create Pack-GroupPack.py
itcharge ccf5054
Update 0416. 分割等和子集.md
itcharge 62f1751
Update 01.Knapsack-Problem-01.md
itcharge 072b564
Update 02.Knapsack-Problem-02.md
itcharge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
Templates/10.Dynamic-Programming/Pack-GroupPack.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Solution: | ||
# 思路 1:动态规划 + 二维基本思路 | ||
def groupPackMethod1(self, group_count: [int], weight: [[int]], value: [[int]], W: int): | ||
size = len(group_count) | ||
dp = [[0 for _ in range(W + 1)] for _ in range(size + 1)] | ||
|
||
# 枚举前 i 组物品 | ||
for i in range(1, size + 1): | ||
# 枚举背包装载重量 | ||
for w in range(W + 1): | ||
# 枚举第 i 组物品能取个数 | ||
dp[i][w] = dp[i - 1][w] | ||
for k in range(group_count[i - 1]): | ||
if w >= weight[i - 1][k]: | ||
# dp[i][w] 取所有 dp[i - 1][w - weight[i - 1][k]] + value[i - 1][k] 中最大值 | ||
dp[i][w] = max(dp[i][w], dp[i - 1][w - weight[i - 1][k]] + value[i - 1][k]) | ||
|
||
return dp[size][W] | ||
|
||
# 思路 2:动态规划 + 滚动数组优化 | ||
def groupPackMethod2(self, group_count: [int], weight: [[int]], value: [[int]], W: int): | ||
size = len(group_count) | ||
dp = [0 for _ in range(W + 1)] | ||
|
||
# 枚举前 i 组物品 | ||
for i in range(1, size + 1): | ||
# 逆序枚举背包装载重量 | ||
for w in range(W, -1, -1): | ||
# 枚举第 i 组物品能取个数 | ||
for k in range(group_count[i - 1]): | ||
if w >= weight[i - 1][k]: | ||
# dp[i][w] 取所有 dp[i - 1][w - weight[i - 1][k]] + value[i - 1][k] 中最大值 | ||
dp[w] = max(dp[w], dp[w - weight[i - 1][k]] + value[i - 1][k]) | ||
|
||
return dp[W] | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.