Java 2 ms solution O(1) space and O(n) time using Kadane Algorithm


  • 0
    P
    public int maxProfit(int[] prices) {
            int maxDiffCurrent=0;
            for(int i=1; i<prices.length; i++){
                maxDiffCurrent += Math.max(0, maxDiffCurrent=prices[i]-prices[i-1]);
            }
            return maxDiffCurrent;
        }

Log in to reply
 

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