1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
055-knapsack.py 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 2020-03-03 17:57 . python-100 answer
class Item(object):
def __init__(self, label, value, weight):
self.label = label
self.value = value
self.weight = weight
def __repr__(self):
return self.label + ' v:' + str(self.value) + ' w:' + str(self.weight)
class Knapsack(object):
def fill_knapsack(self, input_items, total_weight):
if input_items is None or total_weight is None:
raise TypeError('input_items or total_weight cannot be None')
if not input_items or total_weight == 0:
return 0
items = list([Item(label='', value=0, weight=0)] + input_items)
num_rows = len(items)
num_cols = total_weight + 1
T = [[None] * num_cols for _ in range(num_rows)]
for i in range(num_rows):
for j in range(num_cols):
if i == 0 or j == 0:
T[i][j] = 0
elif j >= items[i].weight:
T[i][j] = max(items[i].value + T[i - 1][j - items[i].weight],
T[i - 1][j])
else:
T[i][j] = T[i - 1][j]
results = []
i = num_rows - 1
j = num_cols - 1
while T[i][j] != 0:
if T[i - 1][j] == T[i][j]:
i -= 1
elif T[i][j - 1] == T[i][j]:
j -= 1
else:
results.append(items[i])
i -= 1
j -= items[i].weight
return results
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助