Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

The Priced Stairs

Learn
Play
Prove
Problem context and objectives
Mission briefing

Energy-route planner · A runner crosses a line of power stations

Build each decision from smaller outcomes already solved: The Priced Stairs.

Re-solving the same future branches drains exponential energy.

How you win

  1. 1Recognize when Minimum cost climbing stairs matches the clues
  2. 2Keep this true after every move: the two rolling states are the cheapest costs to reach the preceding two positions
  3. 3Reach the result within O(n) time and O(1) space

Rules and pressure

  • Target cost: O(n) time and O(1) space
  • State rule: the two rolling states are the cheapest costs to reach the preceding two positions
Lesson 1 of 3

Live algorithm trace

Minimum cost climbing stairs

Complete execution
1 of 5

Initialize the two legal starting positions with zero paid cost.

1twoBack = oneBack = 0
2for each step position through the top
3costHere = 0 at top, else cost[index]
4current = costHere + min(oneBack, twoBack)
5shift rolling states
6return cost at top
twoBack = 0oneBack = 0
Truth to preserve / Cost target
Truth to preserve

the two rolling states are the cheapest costs to reach the preceding two positions

Cost target

O(n) time and O(1) space

The cheapest arrival at a step comes from one or two steps below. Add the destination step's cost, while the top beyond the array costs nothing.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead