Best Time to Buy and Sell Stock II
Given an array prices where prices[i] is the price of a stock on day i, you may complete as many transactions as you like (buy one and sell one share, repeatedly), but you can hold at most one share at a time and must sell before buying again. Return the maximum profit you can achieve.
Open official problem prompt ↗Find the largest total profit when you can trade repeatedly but hold at most one share at a time.
Like a hiker who earns money for every uphill meter climbed and loses nothing on descents: the smart move is to bank every single climb, no matter how many hills there are.
- Input
- prices = [7, 1, 5, 3, 6, 4]
- Output
- 7
- Why
- Buy at 1 sell at 5 (+4), then buy at 3 sell at 6 (+3); total 7.
1 <= prices.length <= 3 * 10^40 <= prices[i] <= 10^4