Skip to content
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

p06分组背包的一个问题 #1

Open
lmmsoft opened this issue Feb 3, 2012 · 1 comment
Open

p06分组背包的一个问题 #1

lmmsoft opened this issue Feb 3, 2012 · 1 comment

Comments

@lmmsoft
Copy link

lmmsoft commented Feb 3, 2012

“for v=V..0”这一层循环必须在“for 所有的i属于组k”之外。这样才能保证每一组内的物品最多只有一个会被添加到背包中。
这一句话能详细解释下为什么么?

@DavonChen
Copy link

在做了空间复杂度优化的时候,存储状态的一维数组 F 中的每个元素计算结果依赖于上一次循环得到的当前位置和当前位置左侧的元素结果,如果 “for v=V..0”这一层循环在“for 所有的i属于组k”之内,将导致当前位置和当前位置左侧的元素受到同组的其他物品影响。如果不做空间复杂度上的优化则不会发生这样的影响,代码 demo 可见https://github.com/DavonChen/LeetCode/blob/master/src/dynamicPlanning/backpack/GroupBackpack.java

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants