g2101_2200.s2126_destroying_asteroids.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2101_2200.s2126_destroying_asteroids;
// #Medium #Array #Sorting #Greedy #2022_06_08_Time_6_ms_(99.27%)_Space_54.1_MB_(97.81%)
/**
* 2126 - Destroying Asteroids\.
*
* Medium
*
* You are given an integer `mass`, which represents the original mass of a planet. You are further given an integer array `asteroids`, where `asteroids[i]` is the mass of the ith
asteroid.
*
* You can arrange for the planet to collide with the asteroids in **any arbitrary order**. If the mass of the planet is **greater than or equal to** the mass of the asteroid, the asteroid is **destroyed** and the planet **gains** the mass of the asteroid. Otherwise, the planet is destroyed.
*
* Return `true` _if **all** asteroids can be destroyed. Otherwise, return_ `false`_._
*
* **Example 1:**
*
* **Input:** mass = 10, asteroids = [3,9,19,5,21]
*
* **Output:** true
*
* **Explanation:** One way to order the asteroids is [9,19,5,3,21]:
*
* - The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19
*
* - The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38
*
* - The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43
*
* - The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46
*
* - The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67
*
* All asteroids are destroyed.
*
* **Example 2:**
*
* **Input:** mass = 5, asteroids = [4,9,23,4]
*
* **Output:** false
*
* **Explanation:** The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.
*
* After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.
*
* This is less than 23, so a collision would not destroy the last asteroid.
*
* **Constraints:**
*
* * 1 <= mass <= 105
* * 1 <= asteroids.length <= 105
* * 1 <= asteroids[i] <= 105
**/
public class Solution {
public boolean asteroidsDestroyed(int mass, int[] asteroids) {
return helper(mass, 0, asteroids);
}
private boolean helper(long mass, int startIndex, int[] asteroids) {
int smallOrEqualIndex = partition(mass, startIndex, asteroids);
if (smallOrEqualIndex < startIndex) {
return false;
}
if (smallOrEqualIndex >= asteroids.length - 1) {
return true;
}
for (int i = startIndex; i <= smallOrEqualIndex; ++i) {
mass += asteroids[i];
}
return helper(mass, ++smallOrEqualIndex, asteroids);
}
private int partition(long mass, int startIndex, int[] asteroids) {
int length = asteroids.length;
int smallOrEqualIndex = startIndex - 1;
for (int i = startIndex; i < length; ++i) {
if (asteroids[i] <= mass) {
smallOrEqualIndex++;
swap(asteroids, i, smallOrEqualIndex);
}
}
return smallOrEqualIndex;
}
private void swap(int[] array, int i, int j) {
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy