Advertisement
raghuvanshraj

735.cpp

Jul 28th, 2021
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. /**
  2.  *    author:   vulkan
  3.  *    created:  27.07.2021 06:36:32 PM
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. vector<int> asteroidCollision(vector<int> &asteroids) {
  10.     int n = asteroids.size();
  11.     deque<int> st;
  12.     vector<int> ans;
  13.     for (int i = 0; i <= n - 1; ++i) {
  14.         if (asteroids[i] > 0) {
  15.             st.push_back(asteroids[i]);
  16.         } else {
  17.             while (not st.empty() and st.back() < abs(asteroids[i])) {
  18.                 st.pop_back();
  19.             }
  20.             if (not st.empty()) {
  21.                 if (st.back() == abs(asteroids[i])) {
  22.                     st.pop_back();
  23.                 }
  24.             } else {
  25.                 ans.push_back(asteroids[i]);
  26.             }
  27.         }
  28.     }
  29.  
  30.     while (not st.empty()) {
  31.         ans.push_back(st.front());
  32.         st.pop_front();
  33.     }
  34.  
  35.     return ans;
  36. }
  37.  
  38. int main(int argc, char const *argv[]) {
  39.     int n;
  40.     cin >> n;
  41.     vector<int> asteroids(n);
  42.     for (int i = 0; i <= n - 1; ++i) {
  43.         cin >> asteroids[i];
  44.     }
  45.  
  46.     vector<int> ans = asteroidCollision(asteroids);
  47.     for (int x : ans) {
  48.         cout << x << ' ';
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement