Given daily prices, find the maximum profit from one buy-then-sell transaction. The key is a single greedy pass: always buy at the cheapest price seen so farand ask “what would I profit if I sold today?” — updating the best answer whenever a new high margin appears.
Given an array prices where prices[i] is the stock price on day i, return the maximum profit from buying on one day and selling on a later day. You must buy before you sell. If no profit is possible, return 0.
Example: prices = [7, 1, 5, 3, 6, 4]. Buy on day 1 (price 1), sell on day 4 (price 6) → profit = 5. Buying on day 0 and selling on day 2 only yields -2— you can't sell before you buy.
minPrice(cheapest buy seen) and ask “profit if I sell today = price − minPrice”. Update maxProfit whenever that beats the current best. One pass, constant space.minPrice = Infinity and maxProfit = 0. The zero default handles the “no profitable trade” case automatically.price in the array:price < minPrice, update minPrice = price. This day becomes the new candidate buy day.profit = price − minPrice. If it beats maxProfit, update it. We can't update both in the same step because buying and selling on the same day yields zero profit.maxProfit. Guaranteed ≥ 0.Because you can only buy before you sell, the optimal buy day for any sell day i is simply min(prices[0..i-1]). Maintaining that running minimum greedily is both correct and optimal.
[7, 1, 5, 3, 6, 4]. We scan left→right, tracking the minimum price seen so far (buy day) and the best profit we could lock in today (sell day).function maxProfit(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;
for (const price of prices) {
if (price < minPrice) {
minPrice = price; // found a cheaper buy day
} else {
const profit = price - minPrice;
if (profit > maxProfit) {
maxProfit = profit; // found a better sell day
}
}
}
return maxProfit;
}minPrice = Infinity so the very first price always sets the initial minimum. maxProfit = 0 is the correct default when no profitable trade exists.price - minPrice is the profit if we sold today. Update maxProfitif it's better.maxProfit. Because it was initialized to 0and never goes negative, this handles the “prices only decrease” edge case automatically.maxProfit stays 0 (never updated) and we correctly return 0.if (prices[i] > prices[i-1]) profit += prices[i] - prices[i-1].dp[k][hold] tracking best profit with k buys and current hold state.held, sold, resttracking which state we're in each day.bestBuy and bestSell indices and update them whenever maxProfit improves.| "buy before sell, maximize profit" | one-pass running minimum |
| best previous element for each position | maintain a running min/max |
| "prices only go down" edge case | initialize maxProfit = 0, return 0 |
| follow-up: multiple transactions | sum all positive consecutive differences |
function maxProfit(prices: number[]): number {
let minPrice = Infinity;
let maxProfit = 0;
for (const price of prices) {
if (price < minPrice) {
minPrice = price;
} else {
const profit = price - minPrice;
if (profit > maxProfit) maxProfit = profit;
}
}
return maxProfit;
}minPrice (cheapest buy seen so far) and maxProfit (best spread locked in so far).maxProfit?