Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int maxProfit(vector<int>& prices) {
  4. int k=2;
  5. auto s=prices.size();
  6. if(s==0)
  7. return 0;
  8. vector<vector<int>> dp(k+1,vector<int>(s,0));
  9.  
  10. for(int i=1;i<=k;++i)
  11. {
  12. int buy=dp[i-1][0]-prices[0];
  13. for(int j=1;j<s;++j)
  14. {
  15. dp[i][j]=max(dp[i][j-1],prices[j]+buy);
  16. buy=max(buy,dp[i-1][j]-prices[j]);
  17. }
  18. }
  19. return dp[2][s-1];
  20. }
  21. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement