Skip to content

Commit d9cda33

Browse files
committed
Add solution #2126
1 parent 0b9fd38 commit d9cda33

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,763 LeetCode solutions in JavaScript
1+
# 1,764 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1628,6 +1628,7 @@
16281628
2122|[Recover the Original Array](./solutions/2122-recover-the-original-array.js)|Hard|
16291629
2124|[Check if All A's Appears Before All B's](./solutions/2124-check-if-all-as-appears-before-all-bs.js)|Easy|
16301630
2125|[Number of Laser Beams in a Bank](./solutions/2125-number-of-laser-beams-in-a-bank.js)|Medium|
1631+
2126|[Destroying Asteroids](./solutions/2126-destroying-asteroids.js)|Medium|
16311632
2127|[Maximum Employees to Be Invited to a Meeting](./solutions/2127-maximum-employees-to-be-invited-to-a-meeting.js)|Hard|
16321633
2129|[Capitalize the Title](./solutions/2129-capitalize-the-title.js)|Easy|
16331634
2130|[Maximum Twin Sum of a Linked List](./solutions/2130-maximum-twin-sum-of-a-linked-list.js)|Medium|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* 2126. Destroying Asteroids
3+
* https://leetcode.com/problems/destroying-asteroids/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer mass, which represents the original mass of a planet. You are further
7+
* given an integer array asteroids, where asteroids[i] is the mass of the ith asteroid.
8+
*
9+
* You can arrange for the planet to collide with the asteroids in any arbitrary order. If the mass
10+
* of the planet is greater than or equal to the mass of the asteroid, the asteroid is destroyed
11+
* and the planet gains the mass of the asteroid. Otherwise, the planet is destroyed.
12+
*
13+
* Return true if all asteroids can be destroyed. Otherwise, return false.
14+
*/
15+
16+
/**
17+
* @param {number} mass
18+
* @param {number[]} asteroids
19+
* @return {boolean}
20+
*/
21+
var asteroidsDestroyed = function(mass, asteroids) {
22+
asteroids.sort((a, b) => a - b);
23+
24+
let planetMass = BigInt(mass);
25+
for (const asteroid of asteroids) {
26+
if (planetMass < BigInt(asteroid)) return false;
27+
planetMass += BigInt(asteroid);
28+
}
29+
30+
return true;
31+
};

0 commit comments

Comments
 (0)