Skip to content

python/40_dynamic_programming/01_bag.py 中bag_with_max_value函数有误 #473

Open
@Bernadette321

Description

@Bernadette321

使用专栏中的例子weight: 2,2,4,6,3 value = 3,4,8,9,6

if __name__ == '__main__':
    # [(weight, value), ...]
    items_info = [(2, 3), (2, 4), (4, 8), (6, 9), (3, 6)]
    capacity = 9
    print(bag_with_max_value(items_info, capacity))

的结果不是18而是17
检查发现,比如:
image
刚设置了值为4, 而在下一轮的"for cur_weight in range(capacity+1)中又被设置为了3
最后的结果如下,与专栏老师讲的也不相同
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions