โ† Core Algorithms
#3 most askedVery commonEasyGreedy

Best Time to Buy and Sell Stock

New to Greedy? Learn it from scratch first
Greedy Algorithms โ†’
๐Ÿข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

prices[i] is a stock's price on day i. Buy on one day and sell on a later day. Return the biggest profit you can make, or 0 if you can't make any.

[7, 1, 5, 3, 6, 4] โ†’ 5 (buy at 1, sell at 6).

๐Ÿงช Practice

Write your solution, then press Run tests. Explain your approach out loud as you go.

Python
def max_profit(prices):
    pass

Output appears here. Press Run or Ctrl/โŒ˜ + Enter.

Try it first. Struggling for a few minutes is how it sticks.