5 lines Java easy solution


  • 0
    public int maxProfit(int[] prices) {
            int max = 0, min = Integer.MAX_VALUE;
            for (int i = 0; i < prices.length; i++) {
                if (prices[i] < min) min = prices[i];
                else if (prices[i] > min) max = Math.max(prices[i] - min, max);
            }
            return max;
        }
    

Log in to reply
 

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