nikunjsoni

735

Apr 3rd, 2021
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Solution {
  2. public:
  3.     vector<int> asteroidCollision(vector<int>& asteroids) {
  4.         vector<int> state;
  5.         for(int i=0; i<asteroids.size(); i++){
  6.             bool flag = false;
  7.             if(asteroids[i] > 0){
  8.                 state.push_back(asteroids[i]);
  9.                 continue;
  10.             }
  11.             while(!flag && !state.empty() && state.back() > 0 && -state.back() >= asteroids[i]){
  12.                 if(-state.back() == asteroids[i]) flag = true;
  13.                 state.pop_back();
  14.             }
  15.             if(flag) continue;
  16.             if(!state.empty() && state.back() > 0 && -state.back() < asteroids[i])
  17.                 continue;
  18.             else
  19.                 state.push_back(asteroids[i]);
  20.         }
  21.         return state;
  22.     }
  23. };
RAW Paste Data