Advertisement
Fastrail08

Best Time to Buy & Sell Stocks with Cooldown

Jun 20th, 2025
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void getMaximumProfitWithCooldown(int level, int transactionState, int currProfit, int &maxProfit, vector<int> &prices){
  5.     if(level >= prices.size()){
  6.         maxProfit = max(maxProfit, currProfit);
  7.         return;
  8.     }
  9.    
  10.     //buy
  11.     if(transactionState == 0){
  12.         getMaximumProfitWithCooldown(level + 1, 1, currProfit - prices[level], maxProfit, prices);
  13.     }
  14.     //sell
  15.     if(transactionState == 1){
  16.         getMaximumProfitWithCooldown(level + 1, 2, currProfit + prices[level], maxProfit, prices);
  17.     }
  18.     //cooldown
  19.     if(transactionState == 2){
  20.         getMaximumProfitWithCooldown(level + 1, 0, currProfit, maxProfit, prices);
  21.     }
  22.     //don't do anything
  23.     if(transactionState == 0 || transactionState == 1){
  24.         getMaximumProfitWithCooldown(level + 1, transactionState, currProfit, maxProfit, prices);
  25.     }
  26. }
  27.  
  28. int getMaximumProfitWithCooldownMemo(int level, int transactionState, vector<int> &prices, vector<vector<int> > &memo){
  29.     if(level >= prices.size()){
  30.         return 0;
  31.     }
  32.    
  33.     //level - stock price on each day
  34.     /*
  35.     options -
  36.     transactionState = 0; transaction closed (buy)
  37.     transactionState = 1; transaction opened (sell)
  38.     transactionState = 2; transaction in cooldown (can't buy or sell)
  39.     don't do anything on that day
  40.     */
  41.     if(memo[level][transactionState] != -1){
  42.         return memo[level][transactionState];
  43.     }
  44.     int bought = 0, sold = 0, cooldown = 0, na = 0;
  45.     //can only buy when transactionState is closed
  46.     if(transactionState == 0){
  47.         bought = getMaximumProfitWithCooldownMemo(level + 1, 1, prices, memo) - prices[level];
  48.     }
  49.     //can only sell when transactionState is opened
  50.     if(transactionState == 1){
  51.         sold = getMaximumProfitWithCooldownMemo(level + 1, 2, prices, memo) + prices[level];
  52.     }
  53.     //can only cooldown if transactionState is in cooldown
  54.     if(transactionState == 2){
  55.         cooldown = getMaximumProfitWithCooldownMemo(level + 1, 0, prices, memo);
  56.     }
  57.     //can only do nothing when transactionState is either opened or closed.
  58.     //As we are already doing nothing during cooldown, so call will just get repeated if called during cooldown
  59.     //If on a level when on cooldown, doing nothing that day, will change the transactionState = 0, as we passed the cooldown period, but in Doing Nothing call to the next level we maintain the transactionState = transactionState, which won't make sense if on cooldown, as cooldown will end no matter what after the day/level has passed.
  60.     // we can't have a call like (index, 2) -> (index + 1, 2)..as the transactionState would automatically change from 2 to 0 as the day passes.
  61.     if(transactionState == 0 || transactionState == 1){
  62.         na = getMaximumProfitWithCooldownMemo(level + 1, transactionState, prices, memo);
  63.     }
  64.     return memo[level][transactionState] = max(na, max(cooldown, max(sold, bought)));
  65. }
  66.  
  67. int main() {
  68.     // your code goes here
  69.     int n;
  70.     cin >> n;
  71.     vector<int> prices(n);
  72.     for(int i = 0; i < n; i++){
  73.         cin >> prices[i];
  74.     }
  75.     /*
  76.     Recursive call
  77.     */
  78.     int maxProfit = 0;
  79.     getMaximumProfitWithCooldown(0, 0, 0, maxProfit, prices);
  80.     cout << maxProfit << '\n';
  81.    
  82.     /*
  83.     Memo call
  84.     */
  85.     vector<vector<int> > memo(prices.size(), vector<int>(3, -1));
  86.     cout << getMaximumProfitWithCooldownMemo(0, 0, prices, memo) << '\n';
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement