Optimal substructure
When the best solution to a problem is built from the best solutions to its subproblems.
Example
The cheapest path to stair 5 comes from the cheapest to stair 4 or to stair 3, plus the cost of stair 5.
Test yourself: Optimal substructure
When DP works
With costs 1, 10 and 1 on stairs 1, 2 and 3, the cheapest way to reach 3 costs ___.
Land on 1 (costs 1) and jump to 3 (costs 1): 1 + 1 = 2, without landing on 2, which costs 10.
When DP works
When what comes next depends on the previous move, the state can be ___ to recover the no-aftereffect property.
In the staircase without 2 one-step moves in a row, the state goes from [i] to [i, j]: with n = 10, 20 states.
0/2
â–¶ Play