# 309. Best Time to Buy and Sell Stock with Cooldown

## Problem Statement

\


You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.

Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:

* After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).

**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

&#x20;

**Example 1:**

<pre><code><strong>Input: prices = [1,2,3,0,2]
</strong><strong>Output: 3
</strong><strong>Explanation: transactions = [buy, sell, cooldown, buy, sell]
</strong></code></pre>

**Example 2:**

<pre><code><strong>Input: prices = [1]
</strong><strong>Output: 0
</strong></code></pre>

&#x20;

**Constraints:**

* `1 <= prices.length <= 5000`
* `0 <= prices[i] <= 1000`\
  \


## Intuition

```
Just Skip to index+2 rather than index+1

Only this modification in Best Time to Buy and Sell Stock II
```

### Links

[https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)

### Video Links

[https://www.youtube.com/watch?v=IGIe46xw3YY\&ab\_channel=takeUforward](https://www.youtube.com/watch?v=IGIe46xw3YY\&ab_channel=takeUforward)

### Approach 1:

```
Memoization
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int find_ans(vector<int>& prices, int index, bool buy, vector<vector<int>> &dp){

        if(index >= prices.size())
            return 0;

        if(dp[index][buy] != -1)
            return dp[index][buy];

        int profit;

        if(buy){
            profit = max ( -prices[index] + find_ans(prices, index+1, false, dp), 
                                find_ans(prices, index+1, true, dp) );
            /*
                If buy, 
                    two cases, buy/dont buy change flag accordingly
                    
                 else if sell
                    two cases, Sell/Not sell 
            */
        }

        else{
        
            // Index+2 rather than index+1
            profit = max( prices[index] + find_ans(prices, index+2, true, dp) ,
                                    find_ans(prices, index+1, false, dp) );
        }

        return dp[index][buy] = profit;
    }

    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<vector<int>> dp (n, vector<int>(2,-1));

        return find_ans(prices, 0, true, dp);
    }
};
```
{% endcode %}

### Approach 2:

```
Tabulation
```

{% code title="C++" lineNumbers="true" %}
```cpp
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n = prices.size();
        vector<vector<int>> dp (n+2, vector<int>(2,0));

        //Base Case, No need here just added
        dp[n][0] = dp[n][1] = 0;

        for(int index=n-1; index>=0; index--){
            for(int buy=0; buy<=1; buy++){
                int profit;

                if(buy)
                    profit = max ( -prices[index] + dp[index+1][0], dp[index+1][1] );
                
                else
                    profit = max( prices[index] + dp[index+2][1] ,dp[index+1][0] );

                dp[index][buy] = profit;
            }
        }

        return dp[0][1];
    }
};
```
{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Approach 4:

{% code title="C++" lineNumbers="true" %}
```cpp
```
{% endcode %}

### Similar Problems

###

