leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit df8bfcbc736c9cfcdef11294c244652c0d56ad57
parent 8d7622906d0925ef8798f7160d928ae92ceed16e
Author: Jack Sangdahl <jack@pngu.org>
Date:   Sun, 31 May 2026 10:08:49 -0600

2126. destroying asteroids

Diffstat:
Adestroying_asteroids.c | 42++++++++++++++++++++++++++++++++++++++++++
Adestroying_asteroids.py | 29+++++++++++++++++++++++++++++
2 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/destroying_asteroids.c b/destroying_asteroids.c @@ -0,0 +1,42 @@ +// 2126. Destroying Asteroids +#include "leetcode.h" + +/* + * 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 he mass of the i'th 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. + */ + +bool asteroidsDestroyed(int mass, int *asteroids, int asteroidsSize) { + int total = mass, sum[32] = {0}, min[32]; + for (int i = 0; i < 32; i++) + min[i] = INT_MAX; + for (int i = 0; i < asteroidsSize; i++) { + int x = 31 - __builtin_clz(asteroids[i]); + min[x] = fmin(min[x], asteroids[i]); + sum[x] += asteroids[i]; + } + for (int i = 0; i < 32; i++) { + if (min[i] == INT_MAX) + continue; + if (total < min[i]) + return false; + total += sum[i]; + } + return true; +} + +int main() { + int a1[] = {3, 9, 19, 5, 21}, a2[] = {4, 9, 23, 4}; + bool r1 = asteroidsDestroyed(10, a1, ARRAY_SIZE(a1)); + bool r2 = asteroidsDestroyed(5, a2, ARRAY_SIZE(a2)); + printf("%d\n", r1); + assert(r1 == true); + printf("%d\n", r2); + assert(r2 == false); +} diff --git a/destroying_asteroids.py b/destroying_asteroids.py @@ -0,0 +1,29 @@ +# 2126. Destroying Asteroids +from typing import List + +""" +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 he mass of the i'th 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. +""" + + +class Solution: + def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool: + asteroids.sort() + for i in asteroids: + if mass < i: + return False + mass += i + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.asteroidsDestroyed(mass=10, asteroids=[3, 9, 19, 5, 21])) + print(obj.asteroidsDestroyed(mass=5, asteroids=[4, 9, 23, 4]))