Min Cost Climbing Stairs
easy
dynamic-programming
arrays
Given an integer array cost where cost[i] is the toll charged when you step off stair i, return the minimum total toll to reach the top of the floor.
You can start from stair 0 or stair 1, and from any stair you may climb either one or two stairs. Reaching "the top" means stepping beyond the last stair.
Input / output
- Input:
cost: int[] - Output:
int(minimum total toll)
Examples
cost = [10,15,20]returns15because starting at stair1and paying15climbs straight to the top.cost = [1,100,1,1,1,100,1,1,100,1]returns6by hopping over every stair that charges100.cost = [5,10]returns5because you start on stair0, pay5, and climb two stairs to the top.
Constraints
2 <= cost.length <= 10000 <= cost[i] <= 999
Follow-up
Can you solve it with O(1) extra space by keeping only the two most recent subresults instead of a full DP array?
Examples
Example 1
Input: cost = [10,15,20]
Output: 15
Example 2
Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6
Example 3
Input: cost = [5,10]
Output: 5