Advertisement
i_love_rao_khushboo

Best Time to Buy and Sell Stock with Cooldown

Sep 19th, 2022
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.10 KB | None | 0 0
  1. // Problem link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
  2.  
  3. // Useful Reference(s): --->
  4. // https://medium.com/algorithms-and-leetcode/best-time-to-buy-sell-stocks-on-leetcode-the-ultimate-guide-ce420259b323
  5.  
  6. /************************************************************************************************************/
  7.  
  8. // Method 1 (Recursive Approach)
  9.  
  10. #include<bits/stdc++.h>
  11. using namespace std;
  12.  
  13. int solve(int pos, int n, vector<int> &v) {
  14.     // base case for out of bounds
  15.     if(pos >= n) return 0;
  16.    
  17.     // assuming that we are skipping the current day
  18.     int res = solve(pos + 1, n, v);
  19.    
  20.     // assuming that we buy on pos find all the positions
  21.     // where we can sell the stock
  22.     for(int i = pos + 1; i < n; i++) {
  23.         // to gain profit v[i] must > v[pos]
  24.         if(v[i] > v[pos]) {
  25.             // as we have sell the stock on ith day then there is a cooldown
  26.             // of 1 day, so we now start our subproblem from (i + 2)th day
  27.             res = max(res, v[i] - v[pos] + solve(i + 2, n, v));
  28.         }
  29.     }
  30.    
  31.     return res;
  32. }
  33.  
  34. int maxProfit(vector<int> &prices) {
  35.     vector<int> v = prices;
  36.     int n = v.size();
  37.    
  38.     if(n < 2) return 0;
  39.     if(n == 2) return max(0, v[1] - v[0]);
  40.    
  41.     return solve(0, n, v); 
  42. }
  43.  
  44. int main()
  45. {
  46.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  47.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  48.  
  49.     // #ifndef ONLINE_JUDGE
  50.     //     freopen("input.txt", "r", stdin);
  51.     //     freopen("output.txt", "w", stdout);
  52.     // #endif
  53.  
  54.     vector<int> prices {1, 2, 3, 0, 2};
  55.     cout << maxProfit(prices);
  56.  
  57.     return 0;
  58. }
  59.  
  60. // Time complexity: O(n^n), where n = size of input array
  61.  
  62. /******************************************************************************************************/
  63.  
  64. // Method 2 (Top Down DP)
  65.  
  66. #include<bits/stdc++.h>
  67. using namespace std;
  68.  
  69. int solve(int pos, int n, vector<int> &v, vector<int> &dp) {
  70.     // base case for out of bounds
  71.     if(pos >= n) return 0;
  72.    
  73.     // check if already calculated or not
  74.     if(dp[pos] != -1) return dp[pos];
  75.    
  76.     // assuming that we are skipping the current day
  77.     int res = solve(pos + 1, n, v, dp);
  78.    
  79.     // assuming that we buy on pos find all the positions
  80.     // where we can sell the stock
  81.     for(int i = pos + 1; i < n; i++) {
  82.         // to gain profit v[i] must > v[pos]
  83.         if(v[i] > v[pos]) {
  84.             // as we have sell the stock on ith day then there is a cooldown
  85.             // of 1 day, so we now start our subproblem from (i + 2)th day
  86.             res = max(res, v[i] - v[pos] + solve(i + 2, n, v, dp));
  87.         }
  88.     }
  89.    
  90.     return dp[pos] = res;
  91. }
  92.  
  93. int maxProfit(vector<int> &prices) {
  94.     vector<int> v = prices;
  95.     int n = v.size();
  96.    
  97.     if(n < 2) return 0;
  98.     if(n == 2) return max(0, v[1] - v[0]);
  99.    
  100.     vector<int> dp(n, -1);
  101.        
  102.     return solve(0, n, v, dp); 
  103. }
  104.  
  105. int main()
  106. {
  107.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  108.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  109.  
  110.     // #ifndef ONLINE_JUDGE
  111.     //     freopen("input.txt", "r", stdin);
  112.     //     freopen("output.txt", "w", stdout);
  113.     // #endif
  114.  
  115.     vector<int> prices {1, 2, 3, 0, 2};
  116.     cout << maxProfit(prices);
  117.  
  118.     return 0;
  119. }
  120.  
  121. // Time complexity:
  122.  
  123. /********************************************************************************************************/
  124.  
  125. // NOTE: I DID NOT COMPLETELY UNDERSTAND HOW THIS APPROACH SATISFIES THE "COOLDOWN" CONSTRAINT.
  126.  
  127. // Method 3 (STATE MACHINE DP)
  128. // Link for explanation of this approach --->
  129. // https://www.youtube.com/watch?v=4wNXkhAky3s
  130.  
  131. #include<bits/stdc++.h>
  132. using namespace std;
  133.  
  134. int solve(int n, vector<int> &v) {
  135.     vector<int> noStock(n);
  136.     vector<int> inHand(n);
  137.     vector<int> sold(n);
  138.    
  139.     noStock[0] = 0;
  140.     inHand[0] = -v[0];
  141.     sold[0] = 0;
  142.    
  143.     for(int i = 1; i < n; i++) {
  144.         noStock[i] = max(noStock[i - 1], sold[i - 1]);
  145.         inHand[i] = max(inHand[i - 1], -v[i] + noStock[i - 1]);
  146.         sold[i] = max(sold[i - 1], v[i] + inHand[i - 1]);
  147.     }
  148.    
  149.     return max(noStock[n - 1], sold[n - 1]);
  150. }
  151.  
  152. int maxProfit(vector<int> &prices) {
  153.     vector<int> v = prices;
  154.     int n = v.size();
  155.    
  156.     if(n < 2) return 0;
  157.     if(n == 2) return max(0, v[1] - v[0]);
  158.    
  159.     return solve(n, v);
  160. }
  161.  
  162. int main()
  163. {
  164.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  165.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  166.  
  167.     // #ifndef ONLINE_JUDGE
  168.     //     freopen("input.txt", "r", stdin);
  169.     //     freopen("output.txt", "w", stdout);
  170.     // #endif
  171.  
  172.     vector<int> prices {1, 2, 3, 0, 2};
  173.     cout << maxProfit(prices);
  174.  
  175.     return 0;
  176. }
  177.  
  178. // Time complexity: O(n)
  179. // Space complexity: O(n), where n = size of input array
  180.  
  181. /*******************************************************************************************************/
  182.  
  183. // NOTE: I DID NOT COMPLETELY UNDERSTAND HOW THIS APPROACH SATISFIES THE "COOLDOWN" CONSTRAINT.
  184.  
  185. // Method 4 (SPACE OPTIMIZED STATE MACHINE DP)
  186. // Link for explanation of this approach --->
  187. // https://www.youtube.com/watch?v=4wNXkhAky3s
  188.  
  189. #include<bits/stdc++.h>
  190. using namespace std;
  191.  
  192. int solve(int n, vector<int> &v) {
  193.     int prevNoStock, prevInHand, prevSold;
  194.     int currNoStock, currInHand, currSold;
  195.    
  196.     prevNoStock = 0;
  197.     prevInHand = -v[0]; // Here we buy a stock on the very first day and have not sold it, yet
  198.     prevSold = 0;
  199.    
  200.     for(int i = 1; i < n; i++) {
  201.         // We have no stock today if we:
  202.         // 1. Had no stock yesterday also, and we didn't do anything about it
  203.         // 2. We sold a stock yesterday
  204.         currNoStock = max(prevNoStock, prevSold);
  205.        
  206.         // We have a stock in hand today if we:
  207.         // 1. Had a stock in hand yesterday as well, and we didn't do anything about it
  208.         // 2. Had no stock yesterday but we bought a stock today
  209.         currInHand = max(prevInHand, -v[i] + prevNoStock); // We subtract v[i] as we bought a stock
  210.        
  211.         // We have sold a stock today, if we:
  212.         // Only had a stock in hand yesterday and we sold it today
  213.         currSold = max(prevSold, v[i] + prevInHand);  // We add v[i] as we sold a stock
  214.        
  215.         // updating the values
  216.         prevNoStock = currNoStock;
  217.         prevInHand = currInHand;
  218.         prevSold = currSold;
  219.     }
  220.    
  221.     return max(prevNoStock, prevSold);
  222. }
  223.  
  224. int maxProfit(vector<int> &prices) {
  225.     vector<int> v = prices;
  226.     int n = v.size();
  227.    
  228.      // There are only 1 or fewer days to trade stocks, so then we cannot make a
  229.      // profit by buying and selling stocks, so we don't buy or sell
  230.      // so we return 0
  231.     if(n < 2) return 0;
  232.    
  233.     // self explanatory
  234.     if(n == 2) return max(0, v[1] - v[0]);
  235.    
  236.     return solve(n, v);
  237. }
  238.  
  239. int main()
  240. {
  241.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  242.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  243.  
  244.     // #ifndef ONLINE_JUDGE
  245.     //     freopen("input.txt", "r", stdin);
  246.     //     freopen("output.txt", "w", stdout);
  247.     // #endif
  248.  
  249.     vector<int> prices {1, 2, 3, 0, 2};
  250.     cout << maxProfit(prices);
  251.  
  252.     return 0;
  253. }
  254.  
  255. // Time complexity: O(n)
  256. // Space complexity: O(1), where n = size of input array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement