Simple and fast accepted solution


  • 0
    E
    public static int maxProfit(int[] prices) {
        int profit = 0;
        for (int i = 0; i < prices.length - 1; i++) {
            int j = i + 1;
            while (j < prices.length && prices[i] < prices[j]) {
                profit = Math.max(profit, prices[j] - prices[i]);
                j++;
            }
        }
    
        return profit;
    }
    

Log in to reply
 

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