We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent daef077 commit b502566Copy full SHA for b502566
1 file changed
knapsack/knapsack.py
@@ -5,7 +5,7 @@
5
https://en.wikipedia.org/wiki/Knapsack_problem
6
"""
7
8
-from functools import lru_cache
+from functools import cache
9
10
11
def knapsack(
@@ -47,7 +47,7 @@ def knapsack(
47
if method == "dp":
48
return _knapsack_dp(capacity, weights, values, allow_repetition)
49
50
- @lru_cache(maxsize=None)
+ @cache
51
def recur(cap: int, idx: int) -> int:
52
if idx == 0 or cap == 0:
53
return 0
@@ -64,7 +64,10 @@ def recur(cap: int, idx: int) -> int:
64
65
66
def _knapsack_dp(
67
- capacity: int, weights: list[int], values: list[int], allow_repetition: bool
+ capacity: int,
68
+ weights: list[int],
69
+ values: list[int],
70
+ allow_repetition: bool,
71
) -> int:
72
"""Iterative dynamic programming version of the knapsack problem."""
73
n = len(weights)
0 commit comments