Back to problems Solve on LeetCode →
← Min Subarray Sum

Best Time to Buy and Sell Stock

LeetCode 121 • Easy • Single Transaction

Buy one day, sell on a later day. Maximize profit = sell_price − buy_price. Track min_price so far; best profit if sold today = price − min_price.

TimeO(n)single pass
SpaceO(1)two variables
Day: Min: Max:
Min so far
Current day
Profit
Min price
Max profit
0
Ready
Press Play. For each day: update min_price = min(min_price, price); profit = price − min_price; max_profit = max(max_profit, profit).