-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2126-DestroyingAsteroids.go
More file actions
71 lines (62 loc) · 3.07 KB
/
2126-DestroyingAsteroids.go
File metadata and controls
71 lines (62 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
// 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
import "fmt"
import "sort"
func asteroidsDestroyed(mass int, asteroids []int) bool {
sort.Ints(asteroids)
for _, asteroid := range asteroids {
if mass < asteroid {
return false
}
mass += asteroid
}
return true
}
func main() {
// 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.
fmt.Println(asteroidsDestroyed(10, []int{3,9,19,5,21})) // true
// 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.
fmt.Println(asteroidsDestroyed(5, []int{4,9,23,4})) // false
}