Skip to content

Commit 3123796

Browse files
authored
Create 03 - Bottom-Up | DP | Approach.cpp
1 parent bb6c423 commit 3123796

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Solution {
2+
public:
3+
// Helper function to calculate maximum profit using dynamic programming (bottom-up approach)
4+
int solve(vector<int>& prices){
5+
int n = prices.size();
6+
7+
// Create a 3D DP table to store the maximum profit at each index, with the options to buy or sell, and the remaining transaction limit.
8+
// dp[i][j][k] represents the maximum profit starting from index i, with buy/sell option j (1 = can buy, 0 = can sell), and k remaining transactions.
9+
// We use n+1 for index to account for the boundary condition where we reach the end of the prices array.
10+
vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2, vector<int>(3, 0)));
11+
12+
// Start iterating from the last day (index = n-1) towards the first day (index = 0)
13+
for(int index = n-1; index >= 0; index--){
14+
// For each index, we check whether we can buy (buy == 1) or sell (buy == 0) and the remaining transaction limit
15+
for(int buy = 0; buy <= 1; buy++){
16+
for(int limit = 1; limit <= 2; limit++){
17+
int profit = 0;
18+
19+
// If we can buy (buy == 1)
20+
if(buy){
21+
// Option 1: Buy the stock today, and move to the next day with the ability to sell and the same remaining transaction limit
22+
int buyStock = -prices[index] + dp[index+1][0][limit];
23+
// Option 2: Skip buying and move to the next day, still with the ability to buy and the same remaining transaction limit
24+
int notBuyStock = 0 + dp[index+1][1][limit];
25+
// Choose the option that gives the maximum profit
26+
profit = max(buyStock, notBuyStock);
27+
}else{
28+
// If we can sell (buy == 0)
29+
// Option 1: Sell the stock today, reduce the transaction limit by 1, and move to the next day with the ability to buy
30+
int sellStock = +prices[index] + dp[index+1][1][limit-1];
31+
// Option 2: Skip selling and move to the next day, still with the ability to sell and the same remaining transaction limit
32+
int notSellStock = 0 + dp[index+1][0][limit];
33+
// Choose the option that gives the maximum profit
34+
profit = max(sellStock, notSellStock);
35+
}
36+
37+
// Store the computed result in the dp table for the current index, buy/sell option, and remaining transaction limit
38+
dp[index][buy][limit] = profit;
39+
}
40+
}
41+
}
42+
43+
// The final result is stored at dp[0][1][2], representing the maximum profit starting from day 0, with the ability to buy, and 2 transactions allowed
44+
return dp[0][1][2];
45+
}
46+
47+
// Main function to return the maximum profit from the given prices array
48+
int maxProfit(vector<int>& prices) {
49+
// Call the solve function and return the result
50+
return solve(prices);
51+
}
52+
};

0 commit comments

Comments
 (0)