Skip to content

Commit b5c47ee

Browse files
committed
Use functools.cache for knapsack memoized helpers
1 parent 171481e commit b5c47ee

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

knapsack/knapsack.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from functools import lru_cache
7+
from functools import cache
88

99

1010
def knapsack(
@@ -37,7 +37,7 @@ def knapsack(
3737
got the weight of 10*5 which is the limit of the capacity.
3838
"""
3939

40-
@lru_cache(maxsize=None)
40+
@cache
4141
def knapsack_recur(capacity: int, counter: int) -> int:
4242
# Base Case
4343
if counter == 0 or capacity == 0:
@@ -93,7 +93,7 @@ def knapsack_with_count(
9393
(2, 2)
9494
"""
9595

96-
@lru_cache(maxsize=None)
96+
@cache
9797
def knapsack_recur(remaining_capacity: int, item_count: int) -> tuple[int, int]:
9898
# Base Case: one empty subset yields value 0.
9999
if item_count == 0 or remaining_capacity == 0:

0 commit comments

Comments
 (0)