Problem 561053 · easy · Phase 05 Advanced Algorithms & Graphs

Toll Ladder

dynamic programming · 1-D dp · min cost

A ladder has rungs numbered from 0. Standing on rung i costs toll[i] coins. You may begin on rung 0 or rung 1 (paying that rung's toll), and from any rung you can climb one or two rungs. The climb ends when you step past the last rung, which costs nothing. Return the minimum total toll.

Examples

Input:  toll = [3, 1, 4, 1, 5]
Output: 2
Explanation: start on rung 1 (pay 1), jump to rung 3 (pay 1), then step off the top.

Input:  toll = [10, 20]
Output: 10

Constraints

  • 2 <= len(toll) <= 10**5
  • 0 <= toll[i] <= 10**4
  • Target complexity: O(n) time; trying every path is exponential.

Goals

  • Express the cheapest way to reach a rung through the two rungs below it
  • Handle the free choice of starting rung and the final step past the top
Starting Python…