Description
Given an array cost where cost[i] is the cost of the ith step, you can start from step 0 or 1. Return the minimum cost to reach the top of the floor (past the last step).
Examples
Input:
cost = [10,15,20]Output:
15Explanation:
Start at index 1, pay 15 to reach the top.
Input:
cost = [5,10]Output:
5Explanation:
This is the minimum case with only 2 steps. You can start at index 0 (cost 5) or index 1 (cost 10). Starting at index 0 costs 5 to reach the top, while starting at index 1 costs 10. The minimum is 5.
Input:
cost = [0,0,1,2,3]Output:
2Explanation:
Start at index 0 (cost 0), jump to index 2 (cost 1), then jump to index 4 (cost 3), then reach the top. Total cost: 0 + 1 + 3 = 4. Alternatively, start at index 1 (cost 0), jump to index 3 (cost 2), then reach the top. Total cost: 0 + 2 = 2, which is optimal.
Constraints
- •
2 ≤ cost.length ≤ 1000 - •
0 ≤ cost[i] ≤ 999