Easy · Arrays

Best Time to Buy and Sell Stock

Given an array prices where prices[i] is a stock's price on day i, return the maximum profit from buying on one day and selling on a later day, or 0 if no trade makes a profit.

Examples

Example 1

[7,1,5,3,6,4]

Output: max profit 5

Example 2

[7,6,4,3,1]

Output: max profit 0

Rebuild it in the studio

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Arrays problems