|
| 1 | + |
| 2 | +# 735. Asteroid Collision |
| 3 | + |
| 4 | +> Difficulty: Medium |
| 5 | +> Tags: Stack, Array, Simulation |
| 6 | +> Source: [LeetCode](https://leetcode.com/problems/asteroid-collision) |
| 7 | +> Related Topics: Stack, Simulation |
| 8 | +
|
| 9 | +## Problem Description |
| 10 | + |
| 11 | +We are given an array `asteroids` of integers representing asteroids in a row. The indices of the asteroids represent their relative position in space. |
| 12 | + |
| 13 | +For each asteroid: |
| 14 | +- The **absolute value** represents its size. |
| 15 | +- The **sign** represents its direction (`positive` means right, `negative` means left). |
| 16 | +- All asteroids move at the **same speed**. |
| 17 | + |
| 18 | +We need to simulate their movement and collisions: |
| 19 | +- When two asteroids collide, the smaller one **explodes**. |
| 20 | +- If they are the same size, **both explode**. |
| 21 | +- Asteroids moving in the same direction **never meet**. |
| 22 | + |
| 23 | +Return the state of the asteroids after all collisions. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## Examples |
| 28 | + |
| 29 | +### Example 1 |
| 30 | + |
| 31 | +**Input:** |
| 32 | +`asteroids = [5,10,-5]` |
| 33 | +**Output:** |
| 34 | +`[5,10]` |
| 35 | +**Explanation:** |
| 36 | +- `10` and `-5` collide → `10` survives |
| 37 | +- `5` and `10` do not collide |
| 38 | + |
| 39 | +### Example 2 |
| 40 | + |
| 41 | +**Input:** |
| 42 | +`asteroids = [8,-8]` |
| 43 | +**Output:** |
| 44 | +`[]` |
| 45 | +**Explanation:** |
| 46 | +- Both `8` and `-8` collide and explode |
| 47 | + |
| 48 | +### Example 3 |
| 49 | + |
| 50 | +**Input:** |
| 51 | +`asteroids = [10,2,-5]` |
| 52 | +**Output:** |
| 53 | +`[10]` |
| 54 | +**Explanation:** |
| 55 | +- `2` and `-5` collide → `-5` survives |
| 56 | +- `10` and `-5` collide → `10` survives |
| 57 | + |
| 58 | +--- |
| 59 | + |
| 60 | +## Constraints |
| 61 | + |
| 62 | +- `2 <= asteroids.length <= 10⁴` |
| 63 | +- `-1000 <= asteroids[i] <= 1000` |
| 64 | +- `asteroids[i] != 0` |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Explanation |
| 69 | + |
| 70 | +We can use a **stack** to simulate the collisions. While traversing the array: |
| 71 | +- If the current asteroid is positive (`>0`), push it onto the stack. |
| 72 | +- If it's negative (`<0`), pop from the stack until either: |
| 73 | + - The stack is empty, |
| 74 | + - The top of the stack is also negative, or |
| 75 | + - The top of the stack is larger (in absolute value) and survives. |
| 76 | +- Equal magnitude causes both to explode. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## Solutions |
| 81 | + |
| 82 | +### Python |
| 83 | + |
| 84 | +```python |
| 85 | +class Solution: |
| 86 | + def asteroidCollision(self, asteroids: List[int]) -> List[int]: |
| 87 | + stk = [] |
| 88 | + for x in asteroids: |
| 89 | + if x > 0: |
| 90 | + stk.append(x) |
| 91 | + else: |
| 92 | + while stk and stk[-1] > 0 and stk[-1] < -x: |
| 93 | + stk.pop() |
| 94 | + if stk and stk[-1] == -x: |
| 95 | + stk.pop() |
| 96 | + elif not stk or stk[-1] < 0: |
| 97 | + stk.append(x) |
| 98 | + return stk |
| 99 | +``` |
| 100 | + |
| 101 | +### Java |
| 102 | + |
| 103 | +```java |
| 104 | +class Solution { |
| 105 | + public int[] asteroidCollision(int[] asteroids) { |
| 106 | + Deque<Integer> stk = new ArrayDeque<>(); |
| 107 | + for (int x : asteroids) { |
| 108 | + if (x > 0) { |
| 109 | + stk.offerLast(x); |
| 110 | + } else { |
| 111 | + while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) { |
| 112 | + stk.pollLast(); |
| 113 | + } |
| 114 | + if (!stk.isEmpty() && stk.peekLast() == -x) { |
| 115 | + stk.pollLast(); |
| 116 | + } else if (stk.isEmpty() || stk.peekLast() < 0) { |
| 117 | + stk.offerLast(x); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return stk.stream().mapToInt(Integer::valueOf).toArray(); |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### C++ |
| 127 | + |
| 128 | +```cpp |
| 129 | +class Solution { |
| 130 | +public: |
| 131 | + vector<int> asteroidCollision(vector<int>& asteroids) { |
| 132 | + vector<int> stk; |
| 133 | + for (int x : asteroids) { |
| 134 | + if (x > 0) { |
| 135 | + stk.push_back(x); |
| 136 | + } else { |
| 137 | + while (!stk.empty() && stk.back() > 0 && stk.back() < -x) { |
| 138 | + stk.pop_back(); |
| 139 | + } |
| 140 | + if (!stk.empty() && stk.back() == -x) { |
| 141 | + stk.pop_back(); |
| 142 | + } else if (stk.empty() || stk.back() < 0) { |
| 143 | + stk.push_back(x); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return stk; |
| 148 | + } |
| 149 | +}; |
| 150 | +``` |
| 151 | +
|
| 152 | +### Go |
| 153 | +
|
| 154 | +```go |
| 155 | +func asteroidCollision(asteroids []int) []int { |
| 156 | + var stk []int |
| 157 | + for _, x := range asteroids { |
| 158 | + if x > 0 { |
| 159 | + stk = append(stk, x) |
| 160 | + } else { |
| 161 | + for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x { |
| 162 | + stk = stk[:len(stk)-1] |
| 163 | + } |
| 164 | + if len(stk) > 0 && stk[len(stk)-1] == -x { |
| 165 | + stk = stk[:len(stk)-1] |
| 166 | + } else if len(stk) == 0 || stk[len(stk)-1] < 0 { |
| 167 | + stk = append(stk, x) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return stk |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +### TypeScript |
| 176 | + |
| 177 | +```ts |
| 178 | +function asteroidCollision(asteroids: number[]): number[] { |
| 179 | + const stk: number[] = []; |
| 180 | + for (const x of asteroids) { |
| 181 | + if (x > 0) { |
| 182 | + stk.push(x); |
| 183 | + } else { |
| 184 | + while (stk.length && stk.at(-1)! > 0 && stk.at(-1)! < -x) { |
| 185 | + stk.pop(); |
| 186 | + } |
| 187 | + if (stk.length && stk.at(-1) === -x) { |
| 188 | + stk.pop(); |
| 189 | + } else if (!stk.length || stk.at(-1)! < 0) { |
| 190 | + stk.push(x); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + return stk; |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +### Rust |
| 199 | + |
| 200 | +```rust |
| 201 | +impl Solution { |
| 202 | + pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> { |
| 203 | + let mut stk = Vec::new(); |
| 204 | + for &x in &asteroids { |
| 205 | + if x > 0 { |
| 206 | + stk.push(x); |
| 207 | + } else { |
| 208 | + while let Some(&top) = stk.last() { |
| 209 | + if top < 0 || top > -x { |
| 210 | + break; |
| 211 | + } |
| 212 | + if top == -x { |
| 213 | + stk.pop(); |
| 214 | + break; |
| 215 | + } |
| 216 | + stk.pop(); |
| 217 | + } |
| 218 | + if stk.is_empty() || *stk.last().unwrap() < 0 { |
| 219 | + stk.push(x); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + stk |
| 224 | + } |
| 225 | +} |
| 226 | +``` |
0 commit comments