Problem · Array
Maximum Profit with at Most K Transactions
Learn this problemProblem statement
You are given the price of one batch of goods on consecutive days. You may complete at most k transactions. A transaction consists of buying once and selling later.
- You must sell before buying again.
- You may hold at most one batch at a time.
- A completed buy-and-sell pair counts as one transaction.
Return the maximum profit possible.
Function
maxProfit(prices: int[], k: int) → intExamples
Example 1
prices = [3,2,6,5,0,3]k = 2return = 7Buy at 2 and sell at 6, then buy at 0 and sell at 3.
Constraints
1 <= prices.length <= 10000 <= k <= 1000 <= prices[i] <= 10^6