|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + // Recursive function to calculate the maximum profit with memoization |
| 4 | + // `prices`: vector of stock prices |
| 5 | + // `index`: current day index |
| 6 | + // `transactionNo`: current transaction number (0-based, where even = buy, odd = sell) |
| 7 | + // `k`: maximum number of transactions allowed |
| 8 | + // `dp`: memoization table to store intermediate results |
| 9 | + int solve(vector<int>& prices, int index, int transactionNo, int k, vector<vector<int>>& dp) { |
| 10 | + // Base case: If we've reached the end of the array or completed all allowed transactions |
| 11 | + if (index == prices.size() || transactionNo == 2 * k) return 0; |
| 12 | + |
| 13 | + // Check if the result for this state is already computed |
| 14 | + if (dp[index][transactionNo] != -1) return dp[index][transactionNo]; |
| 15 | + |
| 16 | + int profit = 0; // Variable to store the profit for the current state |
| 17 | + |
| 18 | + // If the current transaction is a "buy" operation (even transaction number) |
| 19 | + if (transactionNo % 2 == 0) { |
| 20 | + // Option 1: Buy the stock on the current day and move to the next transaction |
| 21 | + int buyStock = -prices[index] + solve(prices, index + 1, transactionNo + 1, k, dp); |
| 22 | + // Option 2: Skip buying and move to the next day |
| 23 | + int notBuyStock = 0 + solve(prices, index + 1, transactionNo, k, dp); |
| 24 | + // Maximize the profit between buying and skipping |
| 25 | + profit = max(buyStock, notBuyStock); |
| 26 | + } else { |
| 27 | + // If the current transaction is a "sell" operation (odd transaction number) |
| 28 | + // Option 1: Sell the stock on the current day and move to the next transaction |
| 29 | + int sellStock = +prices[index] + solve(prices, index + 1, transactionNo + 1, k, dp); |
| 30 | + // Option 2: Skip selling and move to the next day |
| 31 | + int notSellStock = 0 + solve(prices, index + 1, transactionNo, k, dp); |
| 32 | + // Maximize the profit between selling and skipping |
| 33 | + profit = max(sellStock, notSellStock); |
| 34 | + } |
| 35 | + |
| 36 | + // Store the result in the dp table to avoid recomputation |
| 37 | + return dp[index][transactionNo] = profit; |
| 38 | + } |
| 39 | + |
| 40 | + // Function to calculate the maximum profit with at most `k` transactions |
| 41 | + // `k`: maximum number of transactions allowed |
| 42 | + // `prices`: vector of stock prices |
| 43 | + int maxProfit(int k, vector<int>& prices) { |
| 44 | + int n = prices.size(); // Total number of days |
| 45 | + |
| 46 | + // Memoization table to store results for each day and transaction number |
| 47 | + // `dp[i][j]`: Maximum profit achievable from day `i` onwards with transaction number `j` |
| 48 | + vector<vector<int>> dp(n, vector<int>(2 * k, -1)); |
| 49 | + |
| 50 | + // Call the recursive function starting from day 0 and transaction number 0 |
| 51 | + return solve(prices, 0, 0, k, dp); |
| 52 | + } |
| 53 | +}; |
0 commit comments