← All problems

309. Best Time to Buy and Sell Stock with Cooldown

MediumOpen on LeetCodeProblem statement

Problem Statement

309. Best Time to Buy and Sell Stock with Cooldown

Medium


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:

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

 

Example 1:

Input: prices = [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]

Example 2:

Input: prices = [1]
Output: 0

 

Constraints:

Notes / Approach

![image](https://user-images.githubusercontent.com/63473496/209307634-17077dd7-fc2a-462d-88bf-2e21519796c5.png) There are three states, according to the action that you can take. Hence, from there, you can now the profit at a state at time i as: ``` s0[i] = max(s0[i - 1], s2[i - 1]); // Stay at s0, or rest from s2 s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); // Stay at s1, or buy from s0 s2[i] = s1[i - 1] + prices[i]; // Only one way from s1 ``` Then, you just find the maximum of s0[n] and s2[n], since they will be the maximum profit we need (No one can buy stock and left with more profit that sell right :) ) Define base case: s0[0] = 0; // At the start, you don't have any stock if you just rest s1[0] = -prices[0]; // After buy, you should have -prices[0] profit. Be positive! s2[0] = INT_MIN; // Lower base case Here is the code :D ```cpp class Solution { public: int maxProfit(vector& prices){ if (prices.size() <= 1) return 0; vector s0(prices.size(), 0); vector s1(prices.size(), 0); vector s2(prices.size(), 0); s1[0] = -prices[0]; s0[0] = 0; s2[0] = INT_MIN; for (int i = 1; i < prices.size(); i++) { s0[i] = max(s0[i - 1], s2[i - 1]); s1[i] = max(s1[i - 1], s0[i - 1] - prices[i]); s2[i] = s1[i - 1] + prices[i]; } return max(s0[prices.size() - 1], s2[prices.size() - 1]); } }; ```

C++

Source file
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() < 2) return 0;
        int s0 = 0, s1 = -prices[0], s2 = 0;
        for (int i = 1; i < prices.size(); ++i) {
            int last_s2 = s2;
            s2 = s1 + prices[i];
            s1 = max(s0 - prices[i], s1);
            s0 = max(s0, last_s2);
        }
        return max(s0, s2);
    }
};