You have four units of something scarce — millions of pounds of capital, servers, sales staff, hours — and three projects that could each use them. Every project turns resource into return, but with diminishing effect: the first unit does the most good, the second a little less, and so on. How do you split the four units to earn the most in total? There are only fifteen ways here, but with twenty projects and a hundred units the choices run into the billions. Dynamic programming solves the whole family in one tidy sweep — and the trick is to make the resource remaining the state.
Resource-allocation problems have a special shape that DP loves. Let
The objective is separable — a sum of per-project terms — and the only thing tying the
projects together is the shared budget
Treat each project as a stage. The state is the amount of budget
Choose an amount for project
Here are the return tables. Notice each column climbs but flattens — diminishing returns.
| units | |||
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 5 | 4 | 3 |
| 2 | 9 | 7 | 6 |
| 3 | 11 | 11 | 9 |
| 4 | 12 | 13 | 12 |
Stage 3 (project 3 alone). With
Stage 2. Apply
Stage 1. Only the full budget matters:
| total | |||
|---|---|---|---|
| 0 | 0 | 14 | 14 |
| 1 | 5 | 11 | 16 |
| 2 | 9 | 7 | 16 |
| 3 | 11 | 4 | 15 |
| 4 | 12 | 0 | 12 |
The optimum is
The reason spreading the budget can beat piling it into one project is the curve below: each project's return bends over as it is fed more. When a project's marginal gain has fallen below what another project would give for the same unit, you switch — and DP does this bookkeeping automatically.
Very nearly. The knapsack problem is the special case where each project ("item") can take only zero or one unit, and the resource is the knapsack's weight capacity. General resource allocation lets a project absorb many units with its own diminishing return schedule. Both are solved by the identical DP skeleton — stages over the items, state equal to the capacity remaining, a max over how much to commit. Recognising that a new problem is "knapsack-shaped" instantly hands you a solution method, which is why the pattern is worth memorising.
DP shines when the state is a single small number — here, budget remaining. But suppose the
resource were three-dimensional: money, staff, and machine time, each with its own budget. The
state becomes a triple