|
| 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 |
0 commit comments