One buy, one sell (buy before sell), maximize profit. [7,1,5,3,6,4] → buy at 1, sell at 6,
profit 5. (LeetCode 121)
Approach: track the minimum price seen so far; for each day, the potential profit is
price[i] - minPrice. Keep the best profit seen, and update the running minimum as you go.
int minimum = prices[0];int maxProfit = 0;int n = prices.length;
for(int i = 0; i < n; i++){ int cost = prices[i] - minimum; maxProfit = Math.max(maxProfit, cost); minimum = Math.min(minimum, prices[i]);}return maxProfit;Time: O(n). Space: O(1).