Skip to content
BytePatterns

Cheapest Stair Climb

EasyDynamic Programming#bottom-up-dp#rolling-variables~20m

Problem

A staircase charges a toll for stepping on each stair, given as a list where position i holds the toll for stair i. From any stair you may move up one or two stairs, and you may begin from either stair 0 or stair 1 without paying to arrive there. Return the cheapest total toll for reaching the floor just past the last stair.

Examples

Input:  cost = [10, 15, 20]
Output: 15
Why:    start on stair 1, pay 15, then jump two stairs to the top
Input:  cost = [1, 100, 1, 1, 100, 1]
Output: 4
Why:    stepping only on the cheap stairs costs four in total
Input:  cost = [5, 3]
Output: 3
Why:    edge case, starting on the cheaper stair and jumping straight off

Hints

0 / 3

Stuck on the idea rather than the code? Climbing Stairs covers it.