Best Time to Buy and Sell Stock

Easy
arrays Amazon Goldman Sachs Citadel

Given an array prices where prices[i] is the stock price on day i, find the maximum profit from buying on one day and selling on a later day. Return 0 if no profit is possible.

Example

max_profit([7, 1, 5, 3, 6, 4])  # => 5  (buy at 1, sell at 6)

Test Cases

Python Editor

Output

Click "Run" to see results...