As reported: Amazon SDE-1 interview (May 2025), round 2 — a variation of Best Time to Buy and Sell Stock II in which you can hold as many stocks as you wish, after Spiral Matrix.
This is LeetCode 122 — Best Time to Buy and Sell Stock II.
prices[i] is the price of a stock on day i. On each day you may buy and/or sell; you can hold at most one share at a time, but you may buy it and sell it again on the same day. Return the maximum profit you can achieve.
Example 1
prices = [7,1,5,3,6,4]
Output: 7 (buy at 1, sell at 5; buy at 3, sell at 6)
Example 2
prices = [1,2,3,4,5]
Output: 4
Constraints: 1 ≤ prices.length ≤ 3·10⁴; 0 ≤ prices[i] ≤ 10⁴.
Code runner format: LeetCode's test-case format — prices on one line. Implement maxProfit(prices); the harness prints the profit.