Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> asteroidCollision(vector<int>& asteroids) {
- stack<int> s;
- for(int x:asteroids)
- {
- bool f=true;
- if(x>0)
- {
- s.push(x);
- }
- else
- {
- while(!s.empty())
- {
- int y=s.top();
- if(y<0)
- {
- break;
- }
- else
- {
- if(x+y==0)
- {
- s.pop();
- f=false;;
- break;
- }
- else if(x+y<0)
- {
- s.pop();
- }
- else
- {
- f=false;
- break;
- }
- }
- }
- if(f)
- {
- s.push(x);
- }
- }
- }
- int n=s.size();
- vector<int> ans(n);
- for(int i=n-1; i>=0; i--){
- ans[i]=s.top();
- s.pop();
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement