Skip to content

Commit b502566

Browse files
authored
Update knapsack.py
replace lru_cache with cache and fix line length
1 parent daef077 commit b502566

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

knapsack/knapsack.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
https://en.wikipedia.org/wiki/Knapsack_problem
66
"""
77

8-
from functools import lru_cache
8+
from functools import cache
99

1010

1111
def knapsack(
@@ -47,7 +47,7 @@ def knapsack(
4747
if method == "dp":
4848
return _knapsack_dp(capacity, weights, values, allow_repetition)
4949

50-
@lru_cache(maxsize=None)
50+
@cache
5151
def recur(cap: int, idx: int) -> int:
5252
if idx == 0 or cap == 0:
5353
return 0
@@ -64,7 +64,10 @@ def recur(cap: int, idx: int) -> int:
6464

6565

6666
def _knapsack_dp(
67-
capacity: int, weights: list[int], values: list[int], allow_repetition: bool
67+
capacity: int,
68+
weights: list[int],
69+
values: list[int],
70+
allow_repetition: bool,
6871
) -> int:
6972
"""Iterative dynamic programming version of the knapsack problem."""
7073
n = len(weights)

0 commit comments

Comments
 (0)