← All problems

Best Time to Buy and Sell Stock

EasyGreedy

Given an array `prices` where `prices[i]` is the stock price on day i, choose ONE day to buy and a future day to sell to maximize profit. Return the max profit, or 0 if no profit.

Examples

Input: [7,1,5,3,6,4]
Output: 5
Buy day 2 (1), sell day 5 (6).
Input: [7,6,4,3,1]
Output: 0

Hints

Solution

Language:
Loading editor…
Output0 lines
Run your code to see output here.
Click Run all tests to check your solution against 5 test cases.