Best Time to Buy and Sell Stock with Cooldown
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/discuss/75924/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
if(n<=1){
return 0;
}
int A=0;
int B=-prices[0];
int C=0;
for(int i=1;i<prices.size();i++){
int temp=A;
A=max(A,C);
C=B+prices[i];
B=max(B,temp-prices[i]);
}
return max(A,C);
}
};
Comments
Post a Comment