-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.py
More file actions
32 lines (26 loc) · 1.43 KB
/
knapsack.py
File metadata and controls
32 lines (26 loc) · 1.43 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
def solution(capacity, items): # O(M * N)
"""
Given the capacity of the knapsack and items specified by weights and values,
return the maximum summarized value of the items that can be fit in the
knapsack.
Example:
capacity = 5, items(value, weight) = [(60, 5), (50, 3), (70, 4), (30, 2)]
result = 80 (items valued 50 and 30 can both be fit in the knapsack)
>>> solution(5, [(60, 5), (50, 3), (70, 4), (30, 2)])
80
"""
result = [(0, 0)] * (capacity + 1) # O(1)
for value, weight in items: # O(N)
if weight > capacity: # O(1)
continue # O(1)
for i in range(1, len(result)): # O(M)
calc_weight = max(weight + result[i - weight][1], \
result[i][1]) # O(1)
if calc_weight <= i: # O(1)
result[i] = (
max(value + result[i - weight][0], result[i][0]),
calc_weight
) # O(1)
return result[capacity][0] # O(1)
if __name__ == '__main__':
solution(5, [(60, 5), (50, 3), (70, 4), (30, 2)])