Asking for a better solution, my solution is clean and simple and seems not fast enough Time O(n)(beats 11%)


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

Log in to reply
 

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