Advertisement
nikunjsoni

309

Oct 15th, 2021
924
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.36 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int maxProfit(vector<int>& prices) {
  4.         int sold = INT_MIN, hold = INT_MIN, rest=0;
  5.         for(int price: prices){
  6.             int presold = sold;
  7.            
  8.             sold = hold+price;
  9.             hold = max(hold, rest-price);
  10.             rest = max(rest, presold);
  11.         }
  12.         return max(sold, rest);
  13.     }
  14. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement