Best Time to Buy and Sell Stock

Code

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length<=1) return 0;

        int curMin = prices[0];
        int profit = 0;

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

        return profit;
    }
}

Reference

  • Soulmachine