You are given an integer array cost where cost[i] is the cost of stepping on the ith stair. You can climb either one or two steps at a time, and you may start from either index 0 or index 1.
Your task is to return the minimum cost to reach the top of the staircase (just beyond the last stair).
Input: cost = [10,15,20]
Output: 15
Explanation: Start at index 1, pay 15, and take two steps to reach the top.
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Explanation: Stepping on indices 0, 2, 3, 4, 6, 7, and 9 gives a total cost of 6.
Input: cost = [0,0]
Output: 0
Explanation: Both stairs cost nothing, so the top is reached at zero cost.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
10 15 20
15
1 100 1 1 1
3
5 4 3 2 1
6