Skip to content
Algorithm in JavaScript
Basic

2126. Destroying Asteroids

초기 질량으로 행성을 오름차순으로 처리하며 모든 행성을 파괴할 수 있는지 그리디하게 판별하는 방법

Oct 18, 2025 — greedy

문제 설명

풀이 아이디어

해결 전략

풀이

구현

/**
* @param {number} mass
* @param {number[]} asteroids
* @return {boolean}
*/
var asteroidsDestroyed = function(mass, asteroids) {
const N = asteroids.length;
asteroids.sort((a,b) => a - b);
for (const asteroid of asteroids) {
// 충돌
if (mass < asteroid) return false;
// 파괴
mass += asteroid;
}
return true;
};