Best Time to Buy and Sell Stock II
MediumP2Coding
prices[i] is the price of a stock on day i. Each day you may buy or sell, holding at most one share at a time (you can sell and buy again on the same day). Return the maximum total profit you can make; return 0 if no profit is possible.
Example 1
- Input
- prices = [7,1,5,3,6,4]
- Output
- 7
- Why
- Buy at 1, sell at 5 (profit 4); buy at 3, sell at 6 (profit 3).
Example 2
- Input
- prices = [7,6,4,3,1]
- Output
- 0
Constraints
- 1 <= prices.length <= 3 * 10^4
- 0 <= prices[i] <= 10^4
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 10. ⌘Enter runs, ⌘⇧Enter submits.