Skip to content

Commit 2d8c867

Browse files
authored
Merge pull request #71 from NEHA-AMIN/feature/asteroid-collison
Solution #735 - Neha Amin - 16/07/2025
2 parents 3e035f9 + e2d7f28 commit 2d8c867

File tree

4 files changed

+196
-0
lines changed

4 files changed

+196
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# 735. Asteroid Collision
2+
Difficulty: Medium
3+
Category: Stack, Simulation
4+
Leetcode Link: [Problem Link](https://leetcode.com/problems/asteroid-collision/)
5+
6+
---
7+
8+
## 📝 Introduction
9+
You're given a list of integers representing asteroids moving in a row. Each asteroid's value represents its size and direction (positive for right-moving, negative for left-moving). When two asteroids moving in opposite directions collide, the smaller one explodes. If both are equal, both explode. Return the state of the asteroids after all collisions.
10+
Constraints:
11+
- -1000 ≤ asteroids[i] ≤ 1000
12+
- 2 ≤ asteroids.length ≤ 10⁴
13+
14+
---
15+
16+
## 💡 Approach & Key Insights
17+
18+
The core idea is to simulate collisions using a stack. As we traverse the asteroids, we push right-moving ones onto the stack. When encountering a left-moving asteroid, we check if the top of the stack has a right-moving asteroid, triggering a possible collision. This approach allows us to efficiently simulate the process and determine the surviving asteroids.
19+
20+
---
21+
22+
## 🛠️ Breakdown of Approaches
23+
24+
### 1️⃣ Brute Force / Naive Approach
25+
26+
Explanation:
27+
Simulate the collisions with nested loops. For each asteroid, check future asteroids for possible collisions and adjust the list accordingly.
28+
Inefficient due to repeated scanning and insertion/deletion from the middle of the list.
29+
30+
Time Complexity: O(N²) - Nested loop comparisons for every possible collision.
31+
Space Complexity: O(1) - No extra space besides the input list.
32+
33+
Example/Dry Run:
34+
Input: [5, 10, -5]
35+
Step 1 → 5 is pushed
36+
Step 2 → 10 is pushed
37+
Step 3 → -5 collides with 10 → -5 is destroyed
38+
Output: [5, 10]
39+
40+
---
41+
42+
### 2️⃣ Optimized Approach
43+
44+
Explanation:
45+
Use a stack to keep track of surviving asteroids. Iterate through the list and resolve collisions dynamically.
46+
- Push all right-moving asteroids.
47+
- For left-moving asteroids, check if top of stack is right-moving → simulate collision.
48+
- Pop smaller asteroid, remove both if equal, or skip push if current is destroyed.
49+
- Finally, return the stack.
50+
51+
Time Complexity: O(N) - Each asteroid is pushed and popped at most once.
52+
Space Complexity: O(N) - In the worst case, all asteroids are pushed to the stack.
53+
54+
Example/Dry Run:
55+
Input: [10, 2, -5]
56+
Step 1 → 10 is pushed
57+
Step 2 → 2 is pushed
58+
Step 3 → -5 collides with 2 → 2 destroyed
59+
→ -5 collides with 10 → -5 destroyed
60+
Output: [10]
61+
62+
---
63+
64+
### 3️⃣ Best / Final Optimized Approach (if applicable)
65+
66+
Same as optimized above. No further optimization possible beyond O(N) time and space.
67+
68+
---
69+
70+
## 📊 Complexity Analysis
71+
72+
| Approach | Time Complexity | Space Complexity |
73+
|----------------|------------------|------------------|
74+
| Brute Force | O(N²) | O(1) |
75+
| Optimized | O(N) | O(N) |
76+
| Best Approach | O(N) | O(N) |
77+
78+
---
79+
80+
## 📉 Optimization Ideas
81+
82+
- Cannot reduce below O(N) time since every asteroid must be evaluated.
83+
- Space may be reduced by modifying input list in-place (if allowed).
84+
85+
---
86+
87+
## 📌 Example Walkthroughs & Dry Runs
88+
89+
Example:
90+
Input: [8, -8]
91+
Process:
92+
1 → 8 is pushed
93+
2 → -8 collides with 8 → both destroyed
94+
Output: []
95+
96+
Example:
97+
Input: [10, 2, -5]
98+
Process:
99+
1 → 10 pushed
100+
2 → 2 pushed
101+
3 → -5 destroys 2 → then -5 is destroyed by 10
102+
Output: [10]
103+
104+
---
105+
106+
## 🔗 Additional Resources
107+
108+
- [Stack Visualization Tool](https://visualgo.net/en/list)
109+
- [Leetcode Discussion](https://leetcode.com/problems/asteroid-collision/discuss)
110+
111+
Author: Neha Amin
112+
Date: 19/07/2025
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public:
3+
vector<int> asteroidCollision(vector<int>& asteroids) {
4+
std::stack<int> stack;
5+
6+
for (int a : asteroids) {
7+
if (a > 0) {
8+
stack.push(a);
9+
} else {
10+
while (!stack.empty() && stack.top() > 0 && stack.top() < -a) {
11+
stack.pop();
12+
}
13+
14+
if (stack.empty() || stack.top() < 0) {
15+
stack.push(a);
16+
}
17+
18+
if (!stack.empty() && stack.top() == -a) {
19+
stack.pop();
20+
}
21+
}
22+
}
23+
24+
std::vector<int> res(stack.size());
25+
int i = stack.size() - 1;
26+
27+
while (!stack.empty()) {
28+
res[i--] = stack.top();
29+
stack.pop();
30+
}
31+
32+
return res;
33+
}
34+
};
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int[] asteroidCollision(int[] asteroids) {
3+
List<Integer> res = new ArrayList<>();
4+
5+
for (int a : asteroids) {
6+
boolean exploded = false;
7+
8+
while (!res.isEmpty() && a < 0 && res.get(res.size() - 1) > 0) {
9+
int lastAsteroid = res.get(res.size() - 1);
10+
11+
if (-a > lastAsteroid) {
12+
res.remove(res.size() - 1);
13+
continue;
14+
} else if (-a == lastAsteroid) {
15+
res.remove(res.size() - 1);
16+
exploded = true;
17+
break;
18+
} else {
19+
exploded = true;
20+
break;
21+
}
22+
}
23+
24+
if (!exploded) {
25+
res.add(a);
26+
}
27+
}
28+
29+
// Convert List to array
30+
return res.stream().mapToInt(Integer::intValue).toArray();
31+
}
32+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def asteroidCollision(self, asteroids: List[int]) -> List[int]:
3+
4+
res = []
5+
6+
for a in asteroids:
7+
8+
while res and a < 0 < res[-1]:
9+
if -a > res[-1]:
10+
res.pop()
11+
continue
12+
elif -a == res[-1]:
13+
res.pop()
14+
break
15+
else:
16+
res.append(a)
17+
18+
return res

0 commit comments

Comments
 (0)