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

留言

這個網誌中的熱門文章

1041. Robot Bounded In Circle 機器人在圈圈裏面?

382. Linked List Random Node: 隨機選元素從list裡面

2134. Minimum Swaps to Group All 1's Together II 使得所有1連在一起