![]() |
VOOZH | about |
Given an array prices[], where prices[i] represents the price of a stock on the i-th day, and an integer k representing the maximum number of transactions allowed, find the maximum profit that can be earned by performing at most k transactions.
Each transaction consists of one buy and one sell operation, and a new transaction can begin only after the previous one is completed.
Examples:
Input: prices[] = [10, 22, 5, 80], k = 2
Output: 87
Explanation: Buy on 1st day at 10 and sell on 2nd day at 22. Then, again buy on 3rd day at 5 and sell on 4th day at 80. Total profit = 12 + 75 = 87Input: prices[] = [90, 80, 70, 60, 50], k = 1
Output: 0
Explanation: Not possible to earn.
Table of Content
The idea is to recursively explore all possible buy and sell decisions to find the maximum profit. To do this, we use a variable buy, which is set to 1 if we can buy a stock and 0 if we must sell the currently held one.
We start from the 0th index, and for each day i, there are two choices depending on the current state:
- If we can buy (buy == 1): We can either buy the stock today or skip the day.
profit(i, k, 1) = max(-prices[i] + profit(i + 1, k, 0), profit(i + 1, k, 1))- If we can sell (buy == 0): We can either sell the stock today or skip the day.
profit(i, k, 0) = max(prices[i] + profit(i + 1, k - 1, 1), profit(i + 1, k, 0))
The recursion terminates when all days are processed (i >= n) or no transactions remain (k <= 0), returning 0 in such cases.
87
We optimize the recursive solution by storing results of overlapping subproblems in a 3D array dp[i][k][buy], where:
Before computing a state, we check if itβs already solved to avoid recomputation.
Recurrence:
If buy == 1:
dp[i][k][1] = max(-prices[i] + dp[i + 1][k][0], dp[i + 1][k][1])
Else:
dp[i][k][0] = max(prices[i] + dp[i + 1][k - 1][1], dp[i + 1][k][0])
87
The idea is to use tabulation (bottom-up DP) approach and build the solution iteratively.
We create a 3D array dp[n+1][k+1][2], where each state represents:
We fill the table starting from the last day towards the first, since the profit of the current day depends on future states.
For each state, we compute:
- If we can buy β dp[i][k][1] = max(-prices[i] + dp[i+1][k][0], dp[i+1][k][1])
- If we can sell β dp[i][k][0] = max(prices[i] + dp[i+1][k-1][1], dp[i+1][k][0])
Finally, dp[0][K][1] gives the maximum profit starting from day 0 with the option to buy.
87
Since on any day we only depend on the results of the next day, we can reduce the 3D DP table to two 2D arrays β ahead (for day i+1) and curr (for day i).
Each has dimensions [k+1][2], representing remaining transactions and buy/sell state.
For each day (from last to first):
- If we can buy, we take the maximum of:
Buying the stock now β -prices[i] + ahead[k][0]
Skipping β ahead[k][1]- If we can sell, we take the maximum of:
Selling the stock now β prices[i] + ahead[k - 1][1]
Skipping β ahead[k][0]
After processing each day, curr becomes the new ahead. Finally, ahead[k][1] gives the maximum profit possible.
87
Releated Problems
Stock Buy and Sell β Max 2 Transactions Allowed
Stock Buy and Sell - Multiple Transactions Allowed