2Brute2Best
🔥0

Asteroid Collision

Medium·LeetCode #735 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given an array asteroids of asteroids flying in a row. For each one, the absolute value is its size and the sign is its direction: positive moves right, negative moves left. All of them move at the same speed.

When two asteroids meet, the smaller one explodes. If they are the same size, both explode. Two asteroids moving in the same direction never meet.

Return the asteroids that are left after every possible collision, in their original order.

Input

An integer array asteroids (no zeros).

Output

The surviving asteroids, left to right.

Example 1
Input: asteroids = [5, 10, -5]
Output: [5, 10]

-5 crashes into 10 and explodes. 5 and 10 both move right, so they never meet.

Example 2
Input: asteroids = [8, -8]
Output: []

Same size, so both explode.

Example 3
Input: asteroids = [10, 2, -5]
Output: [10]

-5 destroys 2, then crashes into 10 and explodes.

Example 4
Input: asteroids = [3, 5, -6, 2, -1, 4]
Output: [-6, 2, 4]

-6 destroys 5 and then 3. -1 explodes against 2. -6 moves left and 2, 4 move right, so nothing else meets.

Constraints
  • • 2 ≤ asteroids.length ≤ 10^4
  • • −1000 ≤ asteroids[i] ≤ 1000
  • • asteroids[i] ≠ 0
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“the smaller one explodes”
→ A new item can remove earlier ones → survivors can be popped.
“positive moves right, negative moves left”
→ Only (+ then −) meet. The direction check decides whether to pop at all.
“after all collisions”
→ One new asteroid may cause several crashes in a row → a while loop on the top.
03

Approach Complexity Ladder

Brute Force — resolve the first crash, then rescanO(N²)O(N)

Repeatedly find the first adjacent (+, −) pair, resolve it, start over

Optimal — survivor stackO(N)O(N)

Push right-movers; a left-mover pops smaller right-movers off the top in a loop

Brute Force Intuition

Simulate it literally. Scan for the first place where a right-mover is directly followed by a left-mover. Resolve that crash by erasing the smaller one (or both if equal). Then scan again from the start. Stop when no such pair exists.

Recognition rule for this sub-pattern:

Simulate a process where new items interact with the latest survivors (collisions, path folders, min tracking).
Remember this · Stack
Trigger
Matching pairs, nested structure, undo, evaluating expressions, "the last one wins".
Think
Does each new element need to look at the most recent unresolved element?
Key question
What does an item on the stack represent, and when does it get resolved?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Stack translate into production architectures and low-level memory mechanics.