-
-
Notifications
You must be signed in to change notification settings - Fork 50.3k
Expand file tree
/
Copy pathknapsack_memoized_clean.py
More file actions
42 lines (31 loc) · 1008 Bytes
/
knapsack_memoized_clean.py
File metadata and controls
42 lines (31 loc) · 1008 Bytes
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
from functools import cache
def knapsack_memoized(weights: list[int], values: list[int], capacity: int) -> int:
"""
Solve 0/1 knapsack using memoization without global state.
Args:
weights: list of item weights
values: list of item values
capacity: maximum capacity of knapsack
Returns:
Maximum achievable value
>>> knapsack_memoized([1, 3, 4], [10, 20, 30], 4)
30
>>> knapsack_memoized([1, 2, 3], [10, 15, 40], 6)
65
>>> knapsack_memoized([], [], 5)
0
"""
if len(weights) != len(values):
raise ValueError("weights and values must be of same length")
n = len(weights)
@cache
def dp(i: int, remaining: int) -> int:
if i == n or remaining == 0:
return 0
if weights[i] > remaining:
return dp(i + 1, remaining)
return max(
dp(i + 1, remaining),
values[i] + dp(i + 1, remaining - weights[i]),
)
return dp(0, capacity)