C++ 6 lines O(1) one pass


  • 0
    B

    to get the n+3 th step, we can use n+1 th step and go 2 steps, or we can use n+2 th step and go 1 step

    class Solution {
    public:
        int minCostClimbingStairs(vector<int>& cost) {
            int stand1 = cost[0], stand2 = cost[1];
            for(int i = 2; i < cost.size(); i++)
            {
                int stand = min(stand1, stand2) + cost[i];
                stand1 = stand2;
                stand2 = stand;
            }
            
            return min(stand1, stand2);
        }
    };

Log in to reply
 

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