Best Time to Buy and Sell Stock IV
Return the maximum profit with at most k transactions.
Examples
Input: [2,[2,4,1]]
Output: 2
Input: [2,[3,2,6,5,0,3]]
Output: 7
Hints
Use a dynamic programming approach where `dp[i][j]` represents the maximum profit with at most `i` transactions up to day `j`, and track both buy and sell states to optimize space.
For each transaction `i` from 1 to `k`, iterate through prices and update the DP table by considering either holding the stock or selling it, while ensuring no overlapping transactions.
Optimize the DP solution by reducing space complexity to O(n) by maintaining only the previous transaction's states and updating them iteratively for each price.
Related Problems
Best Time to Buy and Sell Stock IV
Return the maximum profit with at most k transactions.