LeetCode 2126: Destroying Asteroids – Java Greedy Algorithm Solution with Dry Run

Learn how to solve LeetCode 2126 Destroying Asteroids using a Greedy Algorithm in Java. Includes sorting intuition, brute force approach, optimized solution, dry run, complexity analysis, interview explanation, and common mistakes.

Krishna Shrivastava
2 views
LinkedInGithubX
0
0
LeetCode 2126: Destroying Asteroids – Java Greedy Algorithm Solution with Dry Run
Listen to articleAudio version
Ad
Advertisement

Introduction

LeetCode 2126 – Destroying Asteroids is a classic greedy algorithm problem that tests your ability to:

  1. Recognize optimal ordering
  2. Use sorting effectively
  3. Apply greedy decision-making
  4. Handle large integer growth
  5. Understand simulation problems

This is a very interview-friendly problem because the optimal strategy is not immediately obvious.

Problem Link

🔗 https://leetcode.com/problems/destroying-asteroids/

Problem Statement

You are given:

  1. An integer mass representing the planet’s initial mass
  2. An array asteroids

Rules:

  1. If planet mass ≥ asteroid mass → asteroid is destroyed
  2. Planet gains asteroid mass
  3. Otherwise → planet gets destroyed

Return:

true -> if all asteroids can be destroyed
false -> otherwise

Example

Input

mass = 10
asteroids = [3,9,19,5,21]

Output

true

Key Observation

The most important insight:

Destroy smaller asteroids first.

Why?

Because every destroyed asteroid increases planet mass.

So destroying smaller asteroids early helps you grow enough to destroy larger ones later.

Intuition

Suppose:

mass = 5
asteroids = [100,1,2]

If you attack:

100 first

You instantly lose.

But if you destroy:

1 → 2

Your mass becomes:

5 + 1 + 2 = 8

Still not enough for 100.

So answer remains false.

This demonstrates:

Ordering matters.

Greedy Strategy

The optimal strategy is:

Step 1

Sort asteroids in ascending order.

Step 2

Destroy the smallest asteroid possible first.

Step 3

Keep increasing mass.

Why Sorting Works

If you cannot destroy the smallest remaining asteroid:

You definitely cannot destroy larger asteroids either.

That is why sorting guarantees the optimal greedy order.

Java Solution

class Solution {

public boolean asteroidsDestroyed(int mass, int[] asteroids) {

Arrays.sort(asteroids);

if(mass >= asteroids[asteroids.length - 1]) {
return true;
}

for(int i = 0; i < asteroids.length; i++) {

if(mass >= asteroids[asteroids.length - 1]) {
return true;
}

if(asteroids[i] <= mass) {
mass += asteroids[i];
}

if(mass < asteroids[i]) {
return false;
}
}

return true;
}
}

Cleaner Optimized Version

One important improvement:

Use long instead of int.

Why?

Because mass can grow very large.

Optimized Java Solution

class Solution {

public boolean asteroidsDestroyed(int mass, int[] asteroids) {

Arrays.sort(asteroids);

long currentMass = mass;

for(int asteroid : asteroids) {

if(currentMass < asteroid) {
return false;
}

currentMass += asteroid;
}

return true;
}
}

Why Use Long?

Constraints allow:

1 <= asteroids.length <= 100000
1 <= asteroid[i] <= 100000

Mass can exceed:

Integer.MAX_VALUE

Using long prevents overflow issues.

Dry Run

Input

mass = 10
asteroids = [3,9,19,5,21]

Step 1: Sort

[3,5,9,19,21]

Step 2: Destroy Asteroids

Destroy 3

10 >= 3
mass = 13

Destroy 5

13 >= 5
mass = 18

Destroy 9

18 >= 9
mass = 27

Destroy 19

27 >= 19
mass = 46

Destroy 21

46 >= 21
mass = 67

All asteroids destroyed.

Final Output

true

Brute Force Approach

A brute force approach would try:

All possible asteroid orders

This means:

N! permutations

Which is impossible for:

N = 100000

So brute force is completely infeasible.

Why Greedy Is Optimal

Greedy works because:

  1. Smaller asteroids are easiest to destroy
  2. They increase mass
  3. Increased mass helps destroy bigger asteroids

This creates a natural optimal progression.

Time Complexity Analysis

Sorting

O(N log N)

Traversal

O(N)

Total Time Complexity

O(N log N)

Space Complexity

O(1)

Ignoring sorting space.

Interview Explanation

In interviews, explain:

Since destroying an asteroid increases our mass, the optimal strategy is to destroy smaller asteroids first. Sorting ensures we always maximize future growth opportunities.

This demonstrates:

  1. Greedy thinking
  2. Sorting optimization
  3. Simulation handling
  4. Proof of correctness

Common Mistakes

1. Not Sorting

Without sorting:

You may attempt larger asteroids too early.

2. Using int Instead of long

Mass can overflow.

Always use:

long currentMass

3. Brute Force Permutations

Trying all orders leads to:

O(N!)

which is impossible.

4. Incorrect Early Return

Your logic should only fail when:

currentMass < asteroid

FAQs

Q1. Why is sorting necessary?

Sorting guarantees we always destroy the smallest possible asteroid first.

Q2. Is this a greedy problem?

Yes.

The optimal local decision:

Destroy smallest asteroid first

leads to global optimality.

Q3. Why use long?

Because mass can grow beyond integer limits.

Q4. Is this asked in interviews?

Yes.

It is a common greedy + sorting interview problem.

Conclusion

LeetCode 2126 is an excellent greedy problem for mastering:

  1. Sorting-based optimization
  2. Greedy decision-making
  3. Simulation problems
  4. Overflow handling
  5. Interview reasoning

The key insight is:

Always destroy smaller asteroids first to maximize future mass growth.

Once this greedy intuition becomes natural, many scheduling and optimization problems become easier to solve.

Ai Assistant Kas