From 9dada3e626834a2c2f85cfb4acb00e99f8516212 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Mon, 1 Jun 2026 00:49:32 +0530 Subject: [PATCH] feat: POTD 2026-05-31 solved --- src/2100-2199/2126.cpp | 96 ++++++++++++++++++++++++++++++++++++++++ tests/2100-2199/2126.txt | 15 +++++++ 2 files changed, 111 insertions(+) create mode 100644 src/2100-2199/2126.cpp create mode 100644 tests/2100-2199/2126.txt diff --git a/src/2100-2199/2126.cpp b/src/2100-2199/2126.cpp new file mode 100644 index 0000000..88fbb20 --- /dev/null +++ b/src/2100-2199/2126.cpp @@ -0,0 +1,96 @@ +/* +Tags +lever-medium +array, greedy, sorting + + +Problem Description + +2126. Destroying Asteroids +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 <= 10^5 +1 <= asteroids.length <= 10^5 +1 <= asteroids[i] <= 10^5 +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../core/core.h" +using namespace std; + +class Solution { +public: + bool asteroidsDestroyed(int mass, vector& asteroids) { + long long result = mass; + sort(asteroids.begin(), asteroids.end()); + for (auto& it : asteroids) + { + if (it > result) + { + return false; + } + result += it; + } + return true; + } +}; + +int main() +{ + int mass; + int n; + int x; + vector asteroids; + cin >> mass; + cin >> n; + for (int i = 0; i < n; i++) + { + cin >> x; + asteroids.push_back(x); + } + + Solution sol; + bool result = sol.asteroidsDestroyed(mass, asteroids); + cout << result << "\n"; + return 0; +} \ No newline at end of file diff --git a/tests/2100-2199/2126.txt b/tests/2100-2199/2126.txt new file mode 100644 index 0000000..69e5974 --- /dev/null +++ b/tests/2100-2199/2126.txt @@ -0,0 +1,15 @@ +Test Case 1: +Input: +10 +5 +3 9 19 5 21 +Output: +1 + +Test Case 2: +Input: +5 +4 +4 9 23 4 +Output: +0 \ No newline at end of file