← DiffPush

Best Time to Buy and Sell Stock IV

DiffPush Tier Dynamic Programming · DP on Stocks O(n*k) · O(k)

The Fleet-Wide Charter Quota

The regulator now issues the broker a quota of k lock-release cycles per quarter. The fleet's scheduling terminal must allocate those k cycles across the fee curve to harvest the largest combined spread, never overlapping two cycles. When the quota exceeds the number of profitable swings, the cap effectively disappears.

Input: An integer k and an array prices where prices[i] is the fee on hour i.

Output: Return the maximum profit using at most k non-overlapping buy-sell transactions.

Constraints

Examples

Example 1

Input: {"k":2,"prices":[2,4,1]}
Output: 2
The only climb is 2 to 4; one cycle captures it all.

Example 2

Input: {"k":2,"prices":[3,2,6,5,0,3]}
Output: 7
(2 to 6) plus (0 to 3) — both cycles inside the quota.

Solve this in your browser →

Also on LeetCode ↗