class Solution {
/**
there are only two ways to arrive at step i:
From step i-1
From step i-2
Path 1: From i-1
Cost to reach i-1 is prev1.
you step on i, so you must pay cost[i].
total cost: prev1 + cost[i]
Path 2: From i-2
Cost to reach i-2 is prev2.
you step on i, so you must pay cost[i].
total cost: prev2 + cost[i]
best way to reach this ith step = cost of this ith step + the cheapest way to reach the previous reachable step.
currentCost = cost[i] + Math.min(prev1, prev2)
And update both prev1 and prev2 accordingly
*/
public int minCostClimbingStairs(int[] cost) {
int prev1 = cost[1];
int prev2 = cost[0];
for(int i=2; i<cost.length; i++)
{
int currentCost
= cost
[i
] + Math.
min(prev1, prev2
); prev2 = prev1;
prev1 = currentCost;
}
return Math.
min(prev1, prev2
); }
}