DiffPush Tier Dynamic Programming · DP on Stocks O(n) · O(1)
Fuel regulations cap the broker at two complete lock-release cycles for the season, one cargo unit at a time, never overlapping. Dispatch must decide when each of the two cycles opens and closes to maximize total spread. The planning system walks the fee curve while remembering how many cycles remain.
Input: An array prices where prices[i] is the fee on hour i.
Output: Return the maximum profit using at most two non-overlapping buy-sell transactions.
1 <= len(prices) <= 10^50 <= prices[i] <= 10^5Input: {"prices":[3,3,5,0,0,3,1,4]}
Output: 6
(0 to 3) plus (1 to 4) — two cycles worth 3 each.
Input: {"prices":[1,2,3,4,5]}
Output: 4
One cycle across the whole climb is already optimal; a second adds nothing.