AlgoFlow
advancedRecursion & Dynamic Programming

Climbing stairs with a cost

You are climbing a staircase of n steps. Each step has a cost. You may climb 1 or 2 steps at a time. Return the minimum total cost to reach the top.

Examples

input n = 4, cost = [10, 15, 20, 25]

output 30

Step 1 (10) then jump two to step 3 (20), then to the top: 10 + 20.

input n = 3, cost = [1, 100, 1]

output 2

Take the cheap first and last steps and jump over the expensive middle.

Hints

Try the problem first. Hints are revealed one at a time so you keep thinking.

Solution

The written approach, complexity analysis and follow-ups stay hidden until you ask for them.