Skip to the workspace
Pebellslearn by building
Planning DP  ›  Lesson

Knapsack Variants & the Van Loader

Knapsack Variants 35 minOne table, many knapsacks: change the loop or add a dimension

dsa / Knapsack Variants & the Van Loader

Explore each scene at your own pace.

01 · Four dials on one table

Four dials on one table

Knapsack Variants & the Van Loader

Definition
Knapsack dynamic programming fills a table indexed by capacity, where each cell holds the best value reachable within that limit, and each item is either taken or skipped; the loop direction decides whether an item can be used once or many times.
In plain words
You are packing a van with a weight limit. You work out the best load for every smaller limit first, then for each parcel ask whether adding it beats leaving it out. Looping one way lets each parcel go in once; looping the other way lets you reuse it.

In short: every knapsack is one table with four dials you can turn: how many of each item you may take (once, unlimited, or up to c), what a cell means (at most, exactly or at least a capacity), how two choices combine (max, min, or or +), and how many limits apply at once. The loop direction decides "once" versus "unlimited" — downward reads the row before this item touched it, upward lets the same item feed itself again. Everything else about knapsack is naming which four settings your problem needs.

A one-row table folded downward and upward for the same 2 kg, fee-3 parcel: downward gives 0 0 3 3 3 3 3, upward gives 0 0 3 3 6 6 9, with the upward row's repeated climb circled

Meridian's first van of the day leaves at seven. It can carry 12 kg and its cargo bay holds 11 litres, and the morning's parcels each pay a delivery fee. A heavy little parcel of engine parts and a light but bulky roll of carpet are competing for the same van on two different limits at once. Choosing which parcels go is a knapsack problem. It isn't quite the one you have already solved, though.

You built the 0/1 knapsack table for Tunebox and squeezed it into one row by looping capacity downward. This lesson treats that table as the parent of a family. Almost every knapsack you'll meet changes one of four things, and once you can name which one, you can write the variant without memorising it.

Four dials on one table

Dial Choices Example
How many of each item once (0/1), unlimited, or up to c copies one parcel; a crate size in stock forever; five spare tyres
What a cell means at most w, exactly w, at least w a weight limit; a calibration pallet; a ride quota
How cells combine max, min, or, + best fee; fewest crates; is it possible; how many ways
How many limits one capacity, or several kg only; kg and litres; kg and a slot count

Now the van loader is easy to place: once each, at most, max, and two limits. The rest of this lesson turns each dial in turn.

Scene 1 of 11
Project context and further resources
You're building a piece ofMeridian — the day planner
This piece — load_van(): Picks the parcels that earn the most while staying under the van's weight and volume limits.
Scenario Every Meridian van leaves the depot with a weight limit and a cargo-bay volume, and the morning's parcels each pay a delivery fee. The van loader picks the set that earns the most without breaking either limit, and a heavy-but-small parcel competes with a light-but-bulky one on both at once.
load_van.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
load_van(parcels, max_kg, max_litres) → dictThe highest-earning load under both limits: fee, kg, litres and parcel ids in input order.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.