nikunjsoni

309

Oct 15th, 2021
745
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. };
RAW Paste Data