Fill the min-cost table
Standard · Trace the algorithm · about 6 min
Costs are [5, 2, 8, 1, 9, 3]. Use dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]) with dp[0] = dp[1] = 0. Work out each cell before the trace writes it.
Example 1
Input:
{"cost":[5,2,8,1,9,3]}
Output:
6
Constraints
- Answer with whole numbers.
Learn the idea first: The DP recipe: state, transition, base cases, order, answer, a visual lesson.
More The DP recipe: state, transition, base cases, order, answer problems
- Identify the state Warm-up
- Write the transition Standard
- Min cost climbing stairs Standard
- Stair points Standard
- Apply the recipe to a new problem: house robber Challenge