#3 most askedVery commonEasyGreedy
Best Time to Buy and Sell Stock
New to Greedy? Learn it from scratch firstGreedy Algorithms โ
๐ข Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.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.
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.