forked from lanqiao-courses/python-100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path055-knapsack.py
44 lines (39 loc) · 1.44 KB
/
055-knapsack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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