Skip to content
BytePatterns

Best Value Van Load

MediumDynamic Programming#unbounded-knapsack#bottom-up-dp~25m

Problem

A van can carry at most W kilograms. There are several crate types, each with a whole-number weight of at least 1 and a positive value, and the warehouse has as many crates of every type as you want. Return the largest total value you can load without going over W.

Examples

Input:  W = 10, crates (weight, value) = [(3, 5), (4, 7), (6, 11)]
Output: 18
Why:    one 4 kg crate and one 6 kg crate; three 3 kg crates only reach 15
Input:  W = 7, crates = [(2, 3), (5, 9)]
Output: 12
Why:    5 kg plus 2 kg beats three 2 kg crates, which are worth 9
Input:  W = 2, crates = [(3, 5)]
Output: 0
Why:    edge case, the only crate is too heavy, so the van leaves empty

Hints

0 / 3

Stuck on the idea rather than the code? Unbounded Knapsack covers it.