My accept java solution, use profit, and maxProfit to get the maximum profit


  • 0
    Z
    public int maxProfit(int[] prices) {
        if(prices==null||prices.length<2) return 0;
        int profit = 0;
        int maxProfit = 0;
        
        for(int i = 1;i<prices.length;i++){
            profit+=prices[i]-prices[i-1];
            if(profit<0) profit=0;
            if(profit>maxProfit){
               maxProfit = profit;
            }
        }
        return maxProfit;
    }

Log in to reply
 

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