/**
* Problem: LeetCode 2126 - Destroying Asteroids
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
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;
}
}
"""
Problem: LeetCode 2126 - Destroying Asteroids
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool:
asteroids.sort()
current_mass = mass
for asteroid in asteroids:
if current_mass < asteroid:
return False
current_mass += asteroid
return True
/**
* Problem: LeetCode 2126 - Destroying Asteroids
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
bool asteroidsDestroyed(int mass, vector<int>& asteroids) {
sort(asteroids.begin(), asteroids.end());
long long currentMass = mass;
for (int asteroid : asteroids) {
if (currentMass < asteroid) {
return false;
}
currentMass += asteroid;
}
return true;
}
};
/**
* Problem: LeetCode 2126 - Destroying Asteroids
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number} mass
* @param {number[]} asteroids
* @return {boolean}
*/
var asteroidsDestroyed = function(mass, asteroids) {
asteroids.sort((a, b) => a - b);
let currentMass = mass;
for (let i = 0; i < asteroids.length; i++) {
if (currentMass < asteroids[i]) {
return false;
}
currentMass += asteroids[i];
}
return true;
};