Advertisement
i_love_rao_khushboo

Best Time to Buy and Sell Stock II

Sep 18th, 2022
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1. // Problem link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
  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(bool buy, int pos, int n, vector<int> &v) {
  14.     // base case ==> if pos >= n, means elements finished
  15.     // if buy == true && pos == n - 1, we have to return 0
  16.     // as we can't buy the last element, we can either skip it
  17.     // or sell it
  18.     if((pos >= n) || (buy && pos == n - 1)) return 0;
  19.        
  20.     // Now we have 3 choices (skip, buy or sell the current share according to
  21.     // previous call)
  22.    
  23.     // Case 1: if we skip the current element
  24.     int skip = solve(buy, pos + 1, n, v);
  25.    
  26.     // Case 2: if we either buy or sell the current element
  27.     int profit = 0;
  28.    
  29.     // Case 2.1: if we buy the current element
  30.     if(buy) {
  31.         profit = -v[pos] + solve(false, pos + 1, n, v);
  32.     }
  33.    
  34.     // Case 2.2: if we sell the current element
  35.     else {
  36.         profit = v[pos] + solve(true, pos + 1, n, v);
  37.     }
  38.    
  39.     // return by taking whichever is maximum
  40.     return max(skip, profit);
  41. }
  42.  
  43. int maxProfit(vector<int> &prices) {
  44.     vector<int> v = prices;
  45.     int n = v.size();
  46.    
  47.     if(n < 2) return 0;
  48.     if(n == 2) return max(0, v[1] - v[0]);
  49.        
  50.     // buy is passed as true because we can either skip or buy
  51.     // the 1st element we can't sell it
  52.     return solve(true, 0, n, v);
  53. }
  54.  
  55. int main()
  56. {
  57.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  58.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  59.  
  60.     // #ifndef ONLINE_JUDGE
  61.     //     freopen("input.txt", "r", stdin);
  62.     //     freopen("output.txt", "w", stdout);
  63.     // #endif
  64.  
  65.     vector<int> prices {7, 1, 5, 3, 6, 4};
  66.     cout << maxProfit(prices);
  67.  
  68.     return 0;
  69. }
  70.  
  71. // Time complexity: O(2^n), where n = size of input array
  72.  
  73. /************************************************************************************************************/
  74.  
  75. // Method 2 (Top Down DP)
  76.  
  77. #include<bits/stdc++.h>
  78. using namespace std;
  79.  
  80. int solve(bool buy, int pos, int n, vector<int> &dp_sell, vector<int> &dp_buy, vector<int> &v) {
  81.     // base case ==> if pos >= n, means elements finished
  82.     // if buy == true && pos == n - 1, we have to return 0
  83.     // as we can't buy the last element, we can either skip it
  84.     // or sell it
  85.     if((pos >= n) || (buy && pos == n - 1)) return 0;
  86.    
  87.     // check if already calculated or not
  88.     if(buy && dp_buy[pos] != -1) return dp_buy[pos];
  89.     if(!buy && dp_sell[pos] != -1) return dp_sell[pos];
  90.    
  91.     // Now we have 3 choices (skip, buy or sell the current share according to
  92.     // previous call)
  93.    
  94.     // Case 1: if we skip the current element
  95.     int skip = solve(buy, pos + 1, n, dp_sell, dp_buy, v);
  96.    
  97.     // Case 2: if we either buy or sell the current element
  98.     int profit = 0;
  99.    
  100.     // Case 2.1: if we buy the current element
  101.     if(buy) {
  102.         profit = -v[pos] + solve(false, pos + 1, n, dp_sell, dp_buy, v);
  103.         return dp_buy[pos] = max(skip, profit);
  104.     }
  105.    
  106.     // Case 2.2: if we sell the current element
  107.     else {
  108.         profit = v[pos] + solve(true, pos + 1, n, dp_sell, dp_buy, v);
  109.         return dp_sell[pos] = max(skip, profit);
  110.     }
  111.    
  112.     // return by taking whichever is maximum
  113.     return max(skip, profit);
  114. }
  115.  
  116. int maxProfit(vector<int> &prices) {
  117.     vector<int> v = prices;
  118.     int n = v.size();
  119.    
  120.     if(n < 2) return 0;
  121.     if(n == 2) return max(0, v[1] - v[0]);
  122.    
  123.     vector<int> dp_sell(n, -1);
  124.     vector<int> dp_buy(n, -1);
  125.    
  126.     // buy is passed as true because we can either skip or buy
  127.     // the 1st element we can't sell it
  128.     return solve(true, 0, n, dp_sell, dp_buy, v);
  129. }
  130.  
  131. int main()
  132. {
  133.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  134.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  135.  
  136.     // #ifndef ONLINE_JUDGE
  137.     //     freopen("input.txt", "r", stdin);
  138.     //     freopen("output.txt", "w", stdout);
  139.     // #endif
  140.  
  141.     vector<int> prices {7, 1, 5, 3, 6, 4};
  142.     cout << maxProfit(prices);
  143.  
  144.     return 0;
  145. }
  146.  
  147. // Time complexity: O(n)
  148. // Space complexity: O(n), where n = size of input array
  149.  
  150. /**********************************************************************************************************/
  151.  
  152. // Method 3 (Peak-Valley Approach)
  153.  
  154. // Idea behind the below approach --->
  155. // We need to find the local minima and where there is local minima we need to buy the stock,and we need to
  156. // sell the stock when there is a local maxima. But the 1st share prices is to be excluded in this method.
  157.  
  158. #include<bits/stdc++.h>
  159. using namespace std;
  160.  
  161. int maxProfit(vector<int> &prices) {
  162.     vector<int> v = prices;
  163.     int n = v.size();
  164.    
  165.     int profit = 0;
  166.     for(int i = 1; i < n; i++) {
  167.         if(v[i] > v[i - 1]) profit += (v[i] - v[i - 1]);
  168.     }
  169.    
  170.     return profit;
  171. }
  172.  
  173. int main()
  174. {
  175.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  176.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  177.  
  178.     // #ifndef ONLINE_JUDGE
  179.     //     freopen("input.txt", "r", stdin);
  180.     //     freopen("output.txt", "w", stdout);
  181.     // #endif
  182.  
  183.     vector<int> prices {7, 1, 5, 3, 6, 4};
  184.     cout << maxProfit(prices);
  185.  
  186.     return 0;
  187. }
  188.  
  189. // Time complexity: O(n)
  190. // Space complexity: O(1), where n = size of input array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement