forked from das-jishu/algoexpert-data-structures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack-problem.py
42 lines (34 loc) · 954 Bytes
/
knapsack-problem.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
# KNAPSACK PROBLEM
# O(NC) time and space
def knapsackProblem(items, capacity):
# Write your code here.
# return [
# 10, // total value
# [1, 2], // item indices
# ]
knapsack = [[0 for _ in range(capacity + 1)] for _ in range(len(items)+1)]
for i in range(1, len(items) + 1):
for j in range(1, capacity + 1):
value = items[i-1][0]
weight = items[i-1][1]
if weight > j:
knapsack[i][j] = knapsack[i-1][j]
else:
currentCapacity = value + knapsack[i-1][j-weight]
knapsack[i][j] = max(knapsack[i-1][j], currentCapacity)
#print(knapsack)
return buildSequence(knapsack, items)
def buildSequence(knapsack, items):
sequence = []
i = len(knapsack) - 1
j = len(knapsack[0]) - 1
while i > 0:
if knapsack[i][j] == knapsack[i-1][j]:
i -= 1
else:
sequence.append(i-1)
j -= items[i-1][1]
i -= 1
if j == 0:
break
return [knapsack[-1][-1], list(reversed(sequence))]