Algorithm

Problem Name: 735. Asteroid Collision

We are given an array `asteroids` of integers representing asteroids in a row.

For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.

Example 1:

```Input: asteroids = [5,10,-5]
Output: [5,10]
Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
```

Example 2:

```Input: asteroids = [8,-8]
Output: []
Explanation: The 8 and -8 collide exploding each other.
```

Example 3:

```Input: asteroids = [10,2,-5]
Output: [10]
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
```

Constraints:

• `2 <= asteroids.length <= 104`
• `-1000 <= asteroids[i] <= 1000`
• `asteroids[i] != 0`

Code Examples

#1 Code Example with C++ Programming

```Code - C++ Programming```

``````
class Solution {
public:
vector<int> asteroidCollision(vector<int>& asteroids) {
vector<int>s;
for(auto& x: asteroids)
if(s.empty() || s.back()  <  0 || x > 0) s.push_back(x);
else{
while(!s.empty() && s.back() > 0 && abs(x) > s.back()) s.pop_back();
if(s.empty() || s.back() < 0) s.push_back(x);
else if(abs(x) == s.back()) s.pop_back(>;
}
return s;
}
};
``````
Copy The Code &

Input

cmd
asteroids = [5,10,-5]

Output

cmd
[5,10]

#2 Code Example with Java Programming

```Code - Java Programming```

``````
class Solution {
public int[] asteroidCollision(int[] asteroids) {
Stack stack = new Stack<>();
for (int asteroid : asteroids) {
if (asteroid  <  0) {
while (!stack.isEmpty() && stack.peek() > 0 && stack.peek() < Math.abs(asteroid)) {
stack.pop();
}
if (!stack.isEmpty() && stack.peek() > 0) {
if (stack.peek() == Math.abs(asteroid)) {
stack.pop();
}
} else {
stack.push(asteroid);
}
} else {
stack.push(asteroid);
}
}
int[] result = new int[stack.size()];
for (int i = result.length - 1; i >= 0; i--) {
result[i] = stack.pop();
}
return result;
}
}
``````
Copy The Code &

Input

cmd
asteroids = [5,10,-5]

Output

cmd
[5,10]

#3 Code Example with Javascript Programming

```Code - Javascript Programming```

``````
const asteroidCollision = function(asteroids) {
const positive = []
const res = []
for(let i = 0; i  <  asteroids.length; i++) {
if (asteroids[i] > 0) {
positive.push(i)
} else {
const negVal = asteroids[i];

while(positive.length > 0 && asteroids[ positive[positive.length - 1] ] + negVal  <  0 ) {
asteroids[ positive[positive.length - 1] ] = undefined
positive.pop()
}

if (positive.length > 0) {
if (asteroids[ positive[positive.length - 1] ] + negVal > 0) {
asteroids[i] = undefined
} else if(asteroids[ positive[positive.length - 1] ] + negVal === 0) {
asteroids[i] = undefined
asteroids[ positive[positive.length - 1] ] = undefined
positive.pop()
}
}
}
}
return asteroids.filter(el => el !== undefined)
};
``````
Copy The Code &

Input

cmd
asteroids = [8,-8]

Output

cmd
[]

#4 Code Example with Python Programming

```Code - Python Programming```

``````
class Solution:
def asteroidCollision(self, asteroids):
"""
:type asteroids: List[int]
:rtype: List[int]
"""
stack = []
for asteroid in asteroids:
stack.append(asteroid)
while len(stack) > 1 and stack[-2] > 0 and stack[-1] < 0:
if stack[-2] < abs(stack[-1]): stack[-2] = stack[-1]
elif stack[-2] == abs(stack[-1]): stack.pop()
stack.pop()
return stack
``````
Copy The Code &

Input

cmd
asteroids = [8,-8]

Output

cmd
[]

#5 Code Example with C# Programming

```Code - C# Programming```

``````
using System.Collections.Generic;

namespace LeetCode
{
public class _0735_AsteroidCollision
{
public int[] AsteroidCollision(int[] asteroids)
{
var stack = new Stack < int>();
foreach (var asteroid in asteroids)
{
var explodeBoth = false;

while (stack.Count > 0 && asteroid  <  0 && stack.Peek() > 0)
{
if (stack.Peek() + asteroid < 0)
{
stack.Pop();
continue;
}
else if (stack.Peek() + asteroid == 0)
stack.Pop();

explodeBoth = true;
break;
}

if (!explodeBoth)
stack.Push(asteroid);
}

var result = new int[stack.Count];
for (int i = stack.Count - 1; i >= 0; i--)
result[i] = stack.Pop();

return result;
}
}
}
``````
Copy The Code &

Input

cmd
asteroids = [10,2,-5]

Output

cmd
[10]