Leetcode Problem 2126. Destroying Asteroids

2126. Destroying Asteroids

Leetcode Solutions

Sorting and Greedy Approach

  1. Sort the asteroids array in non-decreasing order.
  2. Initialize a variable current_mass to the value of mass.
  3. Iterate over the sorted asteroids array. a. If current_mass is greater than or equal to the current asteroid's mass, add the asteroid's mass to current_mass. b. If current_mass is less than the current asteroid's mass, return false.
  4. If all asteroids are iterated over without returning false, return true.
UML Thumbnail

Priority Queue (Heap) Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...