Best Time to Buy and Sell Stock IV
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k. Find the maximum profit you can achieve. You may complete at most k transactions. Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

30:00

Best Time to Buy and Sell Stock IV
hard
Topics
Companies

You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k. Find the maximum profit you can achieve. You may complete at most k transactions. Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Example 1:
Input: {"k":2,"prices":[3,2,6,5,0,3]}
Output: 7
Constraints:
  • 1k1001 \leq k \leq 100

  • 1prices.length10001 \leq \text{prices.length} \leq 1000

  • 0prices[i]10000 \leq \text{prices}[i] \leq 1000

Input
arr ={"k":2,"prices":[3,2,6,5,0,3]}

Initialize: k=2 transactions, n=6 days

k = 2 transactions
Stock Prices
3
0
2
1
6
2
5
3
0
4
3
5
buy[] (min cost to buy for i-th transaction)
0INF
1INF
2INF
sell[] (max profit after i-th transaction)
00
10
20
Maximum Profit with at most 2 transactions

0

Current Day
Buy Cost
Sell Profit
Variables
No variables to display
DepthFunction Call
Stack empty
0/32