Skip to content

Commit a6c39bc

Browse files
committed
Asteroid Collision
1 parent 1f9385e commit a6c39bc

File tree

2 files changed

+188
-0
lines changed

2 files changed

+188
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// 735. Asteroid Collision
2+
// Topics: 'Stack', 'Simulation', 'Array'
3+
// Level: 'Medium'
4+
5+
// We are given an array asteroids of integers representing asteroids in a row. The indices of the asteriod in the array represent their relative position in space.
6+
7+
// For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
8+
9+
// Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
10+
11+
// Example 1:
12+
13+
// Input: asteroids = [5,10,-5]
14+
// Output: [5,10]
15+
// Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
16+
17+
// Example 2:
18+
19+
// Input: asteroids = [8,-8]
20+
// Output: []
21+
// Explanation: The 8 and -8 collide exploding each other.
22+
23+
// Example 3:
24+
25+
// Input: asteroids = [10,2,-5]
26+
// Output: [10]
27+
// Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
28+
29+
// Constraints:
30+
31+
// 2 <= asteroids.length <= 104
32+
// -1000 <= asteroids[i] <= 1000
33+
// asteroids[i] != 0
34+
35+
package asteroidcollision
36+
37+
import (
38+
"math"
39+
)
40+
41+
func asteroidCollision(asteroids []int) []int {
42+
var stack []int
43+
for _, a := range asteroids {
44+
var destroyed int
45+
for len(stack) > destroyed && (stack[len(stack)-1-destroyed] > 0 && a < 0) {
46+
absAP, absA := math.Abs(float64(stack[len(stack)-1-destroyed])), math.Abs(float64(a))
47+
if absAP == absA {
48+
destroyed++
49+
a = 0
50+
break
51+
}
52+
if absAP > absA {
53+
a = 0
54+
break
55+
}
56+
if absAP < absA {
57+
destroyed++
58+
continue
59+
}
60+
}
61+
if destroyed > 0 {
62+
stack = stack[:len(stack)-destroyed]
63+
}
64+
if a != 0 {
65+
stack = append(stack, a)
66+
}
67+
}
68+
return stack
69+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package asteroidcollision
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestAsteroidCollision(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
asteroids []int
12+
expected []int
13+
}{
14+
{
15+
name: "negative all after single collision",
16+
asteroids: []int{-2, -2, 1, -2},
17+
expected: []int{-2, -2, -2},
18+
},
19+
{
20+
name: "example 1: right moving asteroids with one left",
21+
asteroids: []int{5, 10, -5},
22+
expected: []int{5, 10},
23+
},
24+
{
25+
name: "example 2: equal size collision",
26+
asteroids: []int{8, -8},
27+
expected: []int{},
28+
},
29+
{
30+
name: "example 3: chain collisions",
31+
asteroids: []int{10, 2, -5},
32+
expected: []int{10},
33+
},
34+
{
35+
name: "all moving right",
36+
asteroids: []int{5, 10, 15},
37+
expected: []int{5, 10, 15},
38+
},
39+
{
40+
name: "all moving left",
41+
asteroids: []int{-5, -10, -15},
42+
expected: []int{-5, -10, -15},
43+
},
44+
{
45+
name: "left then right - no collision",
46+
asteroids: []int{-5, -10, 15, 20},
47+
expected: []int{-5, -10, 15, 20},
48+
},
49+
{
50+
name: "multiple collisions with survivor going left",
51+
asteroids: []int{5, 10, -15},
52+
expected: []int{-15},
53+
},
54+
{
55+
name: "left asteroid destroys multiple right asteroids",
56+
asteroids: []int{1, 2, 3, -10},
57+
expected: []int{-10},
58+
},
59+
{
60+
name: "complex scenario with multiple collisions",
61+
asteroids: []int{-2, -1, 1, 2},
62+
expected: []int{-2, -1, 1, 2},
63+
},
64+
{
65+
name: "single asteroid moving right",
66+
asteroids: []int{10},
67+
expected: []int{10},
68+
},
69+
{
70+
name: "single asteroid moving left",
71+
asteroids: []int{-10},
72+
expected: []int{-10},
73+
},
74+
{
75+
name: "two asteroids same direction",
76+
asteroids: []int{5, 10},
77+
expected: []int{5, 10},
78+
},
79+
{
80+
name: "larger left asteroid wins",
81+
asteroids: []int{10, -15},
82+
expected: []int{-15},
83+
},
84+
{
85+
name: "larger right asteroid wins",
86+
asteroids: []int{15, -10},
87+
expected: []int{15},
88+
},
89+
{
90+
name: "multiple equal collisions",
91+
asteroids: []int{5, -5, 10, -10},
92+
expected: []int{},
93+
},
94+
{
95+
name: "cascade destruction",
96+
asteroids: []int{1, 1, 1, -3},
97+
expected: []int{-3},
98+
},
99+
{
100+
name: "left asteroids followed by larger right",
101+
asteroids: []int{-5, -10, 20},
102+
expected: []int{-5, -10, 20},
103+
},
104+
{
105+
name: "alternating pattern",
106+
asteroids: []int{10, -5, 10, -5},
107+
expected: []int{10, 10},
108+
},
109+
}
110+
111+
for _, tt := range tests {
112+
t.Run(tt.name, func(t *testing.T) {
113+
result := asteroidCollision(tt.asteroids)
114+
if !reflect.DeepEqual(result, tt.expected) {
115+
t.Errorf("asteroidCollision(%v) = %v, want %v", tt.asteroids, result, tt.expected)
116+
}
117+
})
118+
}
119+
}

0 commit comments

Comments
 (0)