Best Time to Buy and Sell Stock IV
Given an integer k and an array prices where prices[i] is the price of a stock on day i, find the maximum profit achievable with at most k transactions. You may not hold more than one share at a time and must sell before buying again.
Open official problem prompt ↗Maximize profit with a configurable ceiling of k round-trip trades.
Like having exactly k reusable train tickets: each ticket is one buy-and-sell journey, and the profit from spending one ticket funds the next, but you can never use more than k in total.
- Input
- k = 2, prices = [3, 2, 6, 5, 0, 3]
- Output
- 7
- Why
- Buy at 2 sell at 6 (+4), then buy at 0 sell at 3 (+3); total 7 with two transactions.
1 <= k <= 1001 <= prices.length <= 10000 <= prices[i] <= 1000