Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- void getMaximumProfitWithCooldown(int level, int transactionState, int currProfit, int &maxProfit, vector<int> &prices){
- if(level >= prices.size()){
- maxProfit = max(maxProfit, currProfit);
- return;
- }
- //buy
- if(transactionState == 0){
- getMaximumProfitWithCooldown(level + 1, 1, currProfit - prices[level], maxProfit, prices);
- }
- //sell
- if(transactionState == 1){
- getMaximumProfitWithCooldown(level + 1, 2, currProfit + prices[level], maxProfit, prices);
- }
- //cooldown
- if(transactionState == 2){
- getMaximumProfitWithCooldown(level + 1, 0, currProfit, maxProfit, prices);
- }
- //don't do anything
- if(transactionState == 0 || transactionState == 1){
- getMaximumProfitWithCooldown(level + 1, transactionState, currProfit, maxProfit, prices);
- }
- }
- int getMaximumProfitWithCooldownMemo(int level, int transactionState, vector<int> &prices, vector<vector<int> > &memo){
- if(level >= prices.size()){
- return 0;
- }
- //level - stock price on each day
- /*
- options -
- transactionState = 0; transaction closed (buy)
- transactionState = 1; transaction opened (sell)
- transactionState = 2; transaction in cooldown (can't buy or sell)
- don't do anything on that day
- */
- if(memo[level][transactionState] != -1){
- return memo[level][transactionState];
- }
- int bought = 0, sold = 0, cooldown = 0, na = 0;
- //can only buy when transactionState is closed
- if(transactionState == 0){
- bought = getMaximumProfitWithCooldownMemo(level + 1, 1, prices, memo) - prices[level];
- }
- //can only sell when transactionState is opened
- if(transactionState == 1){
- sold = getMaximumProfitWithCooldownMemo(level + 1, 2, prices, memo) + prices[level];
- }
- //can only cooldown if transactionState is in cooldown
- if(transactionState == 2){
- cooldown = getMaximumProfitWithCooldownMemo(level + 1, 0, prices, memo);
- }
- //can only do nothing when transactionState is either opened or closed.
- //As we are already doing nothing during cooldown, so call will just get repeated if called during cooldown
- //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.
- // 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.
- if(transactionState == 0 || transactionState == 1){
- na = getMaximumProfitWithCooldownMemo(level + 1, transactionState, prices, memo);
- }
- return memo[level][transactionState] = max(na, max(cooldown, max(sold, bought)));
- }
- int main() {
- // your code goes here
- int n;
- cin >> n;
- vector<int> prices(n);
- for(int i = 0; i < n; i++){
- cin >> prices[i];
- }
- /*
- Recursive call
- */
- int maxProfit = 0;
- getMaximumProfitWithCooldown(0, 0, 0, maxProfit, prices);
- cout << maxProfit << '\n';
- /*
- Memo call
- */
- vector<vector<int> > memo(prices.size(), vector<int>(3, -1));
- cout << getMaximumProfitWithCooldownMemo(0, 0, prices, memo) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement