Example: Best Time to Buy and Sell Stock - rFronteddu/general_wiki GitHub Wiki

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

class Solution {
    public int maxProfit(int[] prices) {
        if (prices == null || prices.length < 2) {
            // less than 2 elements, can't sell
            return 0;
        }

        int minPrice = Integer.MAX_VALUE;
        int maxProfit = 0;

        for (int price : prices) {
            if (price < minPrice) {
                // if I find a min price, from now onward I will use this to find the best profit
                minPrice = price;
            }   
            // update the profit only if bigger than past profit
            int profit = price - minPrice;
            if (profit > maxProfit) {
                maxProfit = profit;
            } 
        }
        return maxProfit;
    }
}