Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions dynamic_programming/knapsack_memoized_clean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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
>>> knapsack_memoized([2, 3, 4], [4, 5, 6], 0)
0
"""

if len(weights) != len(values):
raise ValueError("weights and values must be of same length")

n = len(weights)

@cache
def dp(index: int, remaining: int) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/knapsack_memoized_clean.py, please provide doctest for the function dp

"""
Recursive helper function for knapsack memoization.

Args:
index: current item index
remaining: remaining capacity of knapsack

Returns:
Maximum value achievable from current state

Note:
This function is internally tested via knapsack_memoized doctests.
"""
if index == n or remaining == 0:
return 0

if weights[index] > remaining:
return dp(index + 1, remaining)

return max(
dp(index + 1, remaining),
values[index] + dp(index + 1, remaining - weights[index]),
)

return dp(0, capacity)
Loading