What is Unbounded Knapsack
Same weights and values as 0/1, but each item may be taken any number of times. Walking capacity upward lets a just-written dp[cap-w] already include extra copies of the current item.
Brute force approach
An item may be taken again, so the take branch stays on the same index with a smaller capacity. Capacity repeats across branches. Memoization stores (index, capacity).
At most 2 items and capacity up to 8. Two copies of value 10 weight 2 fill capacity 4 with value 20.
Memoized approach
dp[cap-w] may already include extra copies of the same item.Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.
Interactive DP trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each (cap, item) | O(1) |
| Fill table | O(nW) |
| Space | O(W) |
Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.
Implementations