Best Time to Buy and Sell Stock with Cooldown
Description
Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]Solutions
Optimization
Last updated
Input: [1,2,3,0,2]
Output: 3
Explanation: transactions = [buy, sell, cooldown, buy, sell]Last updated
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n == 0) return 0;
vector<int> stock(n + 1);
vector<int> money(n + 1);
stock[1] = -prices[0];
money[1] = 0;
for(int i = 1; i < n; ++i){
stock[i + 1] = max(stock[i], money[i - 1] - prices[i]);
money[i + 1] = max(stock[i] + prices[i], money[i]);
}
return money[n];
}
};class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n == 0) return 0;
int stock = -prices[0];
int money = 0;
int prev_money = 0;
for(int i = 1; i < n; ++i){
// before:
// stock = stock[i]
// prev_money = money[i - 1]
// after:
// stock = stock[i + 1]
stock = max(stock, prev_money - prices[i]);
// money = money[i]
prev_money = money;
// before:
// money = money[i]
// stock = stock[i + 1]
// if stock[i + 1] = stock[i]
// <==> stock[i] > money[i - 1] - prices[i]
// then it does the same as the previous solution
// else stock[i] < money[i - 1] - prices[i]
// ==> stock[i + 1] = money[i - 1] - prices[i]
// ==> stock + prices[i] = stock[i + 1] + prices[i]
// = money[i - 1] - prices[i] + prices[i + 1]
// = money[i - 1] <= money = money[i]
// thus money[i + 1] must be money[i]
money = max(stock + prices[i], money);
}
return money;
}
};