Short and efficient c++ solution


  • 0
    1
    int maxProfit(vector<int>& prices) {
        int max=0;
        int profit=0;
        int size=prices.size();
        for(int i=1;i<size;++i){
            if(prices[i]-prices[i-1]>0)
               profit+=(prices[i]-prices[i-1]);
            else{
                max+=profit;
                profit=0;
            }
        }
        max+=profit;
        return max;
    }

  • 0

    It's useless to use two variables.


Log in to reply
 

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