C++, o(n) solution, kind of greedy.


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

  • 1
    H

    wrong answer!


Log in to reply
 

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