C++, three liner DP solution

  • 0

    The current step cost depends on the minimum of the last two steps plus current one.

    class Solution {
        int minCostClimbingStairs(vector<int>& cost) {
            // not checking edge cases as cost length is always >=2
            for(int i=2;i<cost.size();++i){
                cost[i] += min(cost[i-1],cost[i-2]);
            return min(cost[cost.size()-1],cost[cost.size()-2]);

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.