Best Time to Buy and Sell Stock IV - Problem

You're a stock trader with limited transactions - you can make at most k complete buy-sell cycles to maximize your profit.

Given an array prices where prices[i] represents the stock price on day i, and an integer k representing the maximum number of transactions allowed, find the maximum profit you can achieve.

Important constraints:

  • You must sell before buying again (no simultaneous transactions)
  • A transaction consists of buying AND then selling a stock
  • You can complete at most k transactions

Example: If prices = [2,4,1] and k = 2, you can buy at 2, sell at 4 (profit = 2), but there's no profitable second transaction, so maximum profit is 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: k = 2, prices = [2,4,1]
โ€บ Output: 2
๐Ÿ’ก Note: Buy at price 2, sell at price 4. Profit = 4 - 2 = 2. No profitable second transaction possible.
example_2.py โ€” Multiple Transactions
$ Input: k = 2, prices = [3,2,6,5,0,3]
โ€บ Output: 7
๐Ÿ’ก Note: Buy at price 2, sell at price 6 (profit = 4). Buy at price 0, sell at price 3 (profit = 3). Total = 7.
example_3.py โ€” Edge Case
$ Input: k = 2, prices = [1,2,3,4,5]
โ€บ Output: 4
๐Ÿ’ก Note: One transaction is optimal: buy at 1, sell at 5 for profit of 4. Multiple smaller transactions don't yield more profit.

Visualization

Tap to expand
Trading Strategy: k=2 transactions, prices=[2,4,1]Transaction 1Buy: -2Sell: 2Transaction 2Buy: -โˆžSell: 0Price Timeline: Day 1 (โ‚น2) โ†’ Day 2 (โ‚น4) โ†’ Day 3 (โ‚น1)BuySellProfit: โ‚น2๐Ÿ’ก Key Insight: Track maximum profit states for each transaction level independently
Understanding the Visualization
1
Initialize Trading Slots
Set up k transaction slots. Each has a 'buy state' (money after buying) and 'sell state' (profit after selling).
2
Process Daily Prices
For each day's price, update all trading slots optimally from highest to lowest transaction number.
3
Update Sell Decisions
For each slot, decide: keep previous sell profit or sell today (buy state + current price).
4
Update Buy Decisions
For each slot, decide: keep previous buy state or buy today (previous transaction's profit - current price).
5
Extract Maximum Profit
The sell state of the k-th transaction contains the maximum possible profit.
Key Takeaway
๐ŸŽฏ Key Insight: By maintaining separate buy/sell states for each transaction level and updating them optimally, we avoid exploring all combinations while guaranteeing the maximum profit.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n*k)

We process each of n prices and update k transaction states

n
2n
โœ“ Linear Growth
Space Complexity
O(k)

We only need to store buy and sell states for k transactions

n
2n
โœ“ Linear Space

Constraints

  • 0 โ‰ค k โ‰ค 100
  • 0 โ‰ค prices.length โ‰ค 1000
  • 0 โ‰ค prices[i] โ‰ค 1000
  • Time limit: 2 seconds per test case
Asked in
Google 42 Amazon 38 Microsoft 31 Apple 24 Meta 19
89.2K Views
High Frequency
~35 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen