Simple DP solution


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

Log in to reply
 

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