public int maxProfit(int[] prices) {
        if (prices == null || prices.length == 0) return 0;
        int min = prices[0];

        int profit = prices[1] - prices[0];

        for(int i = 2; i < prices.length; i++) {
            min = Math.min(prices[i - 1], min);
            profit = Math.max(profit, prices[i] - min);
        }

        if(profit < 0) {
            return 0;
        }

        return profit;
    }

results matching ""

    No results matching ""