Why the best value per kilo is the wrong first pick when items cannot be split.
About this comparison
One 7 kg carry-on and three items: a laptop (3 kg, $600), a drone (4 kg, $900) and a camera bag (5 kg, $1,200). Greedy packs it first, then dynamic programming fills a table of best bags and packs it again.
Greedy sorts the items and takes each one that still fits. By price or by price per kilo the order is the same: the camera bag at $240 a kilo, the drone at $225, the laptop at $200. It packs the camera bag, then tries the drone and the laptop against the 2 kg left. Neither fits, and its counter stops at 3 items tried.
Dynamic programming (DP) first answers smaller questions: the best value for every bag size from 1 to 7 kg, using only the items so far. One row per item, one column per size: 21 cells. Each cell keeps the cell above (skip the item) or takes the item plus the best answer for the kilos it leaves. Then it walks back from the 7 kg cell to read off the items.
Greedy decides once per item and never looks back: O(n log n), for the sort. DP weighs every item against every bag size: O(n · W) time and space, where W is the weight limit. That extra work buys a guarantee when items are taken whole or left behind.
The camera bag fills 5 of 7 kg and strands 2 kg that nothing fits. In the table, the drone reuses the laptop row: its 7 kg cell is $900 plus the best 3 kg bag, $600, so $1,500. The camera bag tries $1,200 plus the best 2 kg bag, $0, and loses. The walk back packs the drone and the laptop: 7 of 7 kg, $300 more in the same bag.
Use greedy when items can be split, as in fractional knapsack, where best value per kilo first is exact. It is also a fair quick estimate when the weight limit is too large for a table.
Use the DP table when items come whole and the best set matters: projects within a budget, cargo by weight. It grows with the limit itself, so a limit in the millions calls for another method.