Best Time to Buy And Sell Stock
Choose one day to buy and a strictly later day to sell. Return the largest possible profit, or zero if no profitable trade exists.
Example 1
prices = [9,2,6,1,7]6Explanation
Buy at 1 on day 3 and sell at 7 on day 4 for profit 6.
Example 2
prices = [5,4,2]0Explanation
Prices only fall, so skipping a trade gives profit zero.
Example 3
prices = [3]0Explanation
One day provides no strictly later selling day.
Constraints
- 1 <= len(prices) <= 100000
- Prices are 0..10000
Function contract
maxProfitArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[9,2,6,1,7]