Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int maxProfit(vector<int>& prices) {
- int k=2;
- auto s=prices.size();
- if(s==0)
- return 0;
- vector<vector<int>> dp(k+1,vector<int>(s,0));
- for(int i=1;i<=k;++i)
- {
- int buy=dp[i-1][0]-prices[0];
- for(int j=1;j<s;++j)
- {
- dp[i][j]=max(dp[i][j-1],prices[j]+buy);
- buy=max(buy,dp[i-1][j]-prices[j]);
- }
- }
- return dp[2][s-1];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement