Leetcode 2126 Destroying Asteroids
Leetcode 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.
這題很簡單
先sort 隕石array
之後一個一個測試
如果不夠大 就return False
夠大
就繼續變大XD
class Solution: def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool: asteroids.sort() start=mass for i in range(len(asteroids)): if start>=asteroids[i]: start+=asteroids[i] else: return False return True
留言
張貼留言