One trade, best profit
Reported in interviews at (where)
prices[d] is a share's price on day d. You may buy one share on one day and sell it on a later day, once.
Return the most money that one trade can make. If no trade makes money, return 0.
- Input
prices = [8,3,6,1,5,4]- Output
4
Buy at 1 on day 3 and sell at 5 on day 4.
- Input
prices = [9,7,4,1]- Output
0
The price only falls, so the best is not to trade.
Limits
1 <= prices.length <= 1000000 <= prices[d] <= 10000
Hints
Hint 1
The best sale on a day buys at the lowest price seen before it.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
