Another solution let time go back


  • 0
    D
    int maxProfit(int* prices, int pricesSize) {
        if(pricesSize == 0 || pricesSize == 1){
            return 0;
        }
        int maxvalue = prices[pricesSize-1];
        int selected;
        int maxprofit = 0, profit = 0;
        for(int i = pricesSize-2; i >= 0; i--){
            profit = maxvalue - prices[i];
            maxprofit = maxprofit > profit ? maxprofit : profit;
            maxvalue = maxvalue > prices[i] ? maxvalue : prices[i];
        }
        return maxprofit;
    

    }


Log in to reply
 

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