Skip to content

Commit 5aa3c86

Browse files
authored
Create 04 - Space Optimized | DP | Approach.cpp
1 parent 9c9b30a commit 5aa3c86

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Solution {
2+
public:
3+
// Function to calculate the maximum profit using space-optimized dynamic programming
4+
// `prices`: vector of stock prices
5+
// `k`: maximum number of transactions allowed
6+
int solve(vector<int>& prices, int k) {
7+
int n = prices.size(); // Total number of days
8+
9+
// Create two 2D vectors `curr` and `next` to store the DP states for the current and next days.
10+
// Each has 2 rows for the buy/sell state and `k+1` columns for the transaction limit.
11+
vector<vector<int>> curr(2, vector<int>(k + 1, 0)); // Current day's DP table
12+
vector<vector<int>> next(2, vector<int>(k + 1, 0)); // Next day's DP table
13+
14+
// Iterate backwards over days (from the last day to the first day)
15+
for (int index = n - 1; index >= 0; index--) {
16+
// Iterate over the buy/sell state (0 = sell, 1 = buy)
17+
for (int buy = 0; buy <= 1; buy++) {
18+
// Iterate over the transaction limits (from 1 to k)
19+
for (int limit = 1; limit <= k; limit++) {
20+
int profit = 0; // Variable to store the profit for the current state
21+
22+
if (buy) {
23+
// If the current state allows buying, we have two options:
24+
// 1. Buy the stock at the current price, then move to the next day with `buy = 0`.
25+
// 2. Skip buying and move to the next day with `buy = 1`.
26+
int buyStock = -prices[index] + next[0][limit]; // Buying
27+
int notBuyStock = 0 + next[1][limit]; // Skipping
28+
profit = max(buyStock, notBuyStock); // Maximize profit
29+
} else {
30+
// If the current state allows selling, we have two options:
31+
// 1. Sell the stock at the current price, then move to the next day with `buy = 1` and decrement the transaction count.
32+
// 2. Skip selling and move to the next day with `buy = 0`.
33+
int sellStock = prices[index] + next[1][limit - 1]; // Selling
34+
int notSellStock = 0 + next[0][limit]; // Skipping
35+
profit = max(sellStock, notSellStock); // Maximize profit
36+
}
37+
38+
// Store the result in the current DP table
39+
curr[buy][limit] = profit;
40+
}
41+
}
42+
43+
// Move the current DP table to the next day (space optimization)
44+
next = curr;
45+
}
46+
47+
// Return the maximum profit starting from day 0, with the ability to buy and `k` transactions available
48+
return curr[1][k];
49+
}
50+
51+
// Main function to calculate the maximum profit
52+
// `k`: maximum number of transactions allowed
53+
// `prices`: vector of stock prices
54+
int maxProfit(int k, vector<int>& prices) {
55+
int n = prices.size(); // Total number of days
56+
57+
// Call the solve function to compute the result using dynamic programming
58+
return solve(prices, k);
59+
}
60+
};

0 commit comments

Comments
 (0)