-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcercise2.py
More file actions
56 lines (39 loc) · 1.15 KB
/
excercise2.py
File metadata and controls
56 lines (39 loc) · 1.15 KB
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
45
46
47
48
49
50
51
52
53
54
55
def thief_which(objects,max_weight,items):
n = len(objects)
weight = []
for i in range(n):
weight.append(objects[i][0])
used_items = []
w = max_weight
for i in range(n):
used_items.append(0)
while w >= 0:
if items[w] == -1:
break
used_items[items[w]] +=1
w = w - weight[items[w]]
return used_items
def thief(objects,max_weight):
weight = []
values = []
items = []
items.append(-1)
n = len(objects)
for i in range(n):
weight.append(objects[i][0])
values.append(objects[i][1])
dp = []
for i in range(max_weight + 1):
dp.append(0)
for i in range(1,max_weight+1):
items.append(items[i-1])
ma = dp[i-1]
for j in range(n):
x = i - weight[j]
if x >=0 and (dp[x] + values[j]) > ma:
ma = dp[x] + values[j]
items[i] = j
dp[i] = ma
return dp[max_weight],items
n,items = thief([(2,50),(3,100),(5,140)],17)
print(thief_which([(2,50),(3,100),(5,140)],17,items))