Skip to content

Commit bcf4c58

Browse files
committed
added tests and description for asteroidCollision
1 parent c928628 commit bcf4c58

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
package asteroid_collision
22

3-
import "testing"
3+
import (
4+
"slices"
5+
"testing"
6+
)
47

5-
func TestSolve(t *testing.T) {
6-
t.Fatal("write tests")
8+
func TestAsteroidCollision(t *testing.T) {
9+
cases := []struct {
10+
asts []int
11+
out []int
12+
}{
13+
{[]int{5, 10, -5}, []int{5, 10}},
14+
{[]int{8, -8}, []int{}},
15+
{[]int{10, 2, -5}, []int{10}},
16+
{[]int{3, 5, -6, 2, -1, 4}, []int{-6, 2, 4}},
17+
}
18+
for _, c := range cases {
19+
res := asteroidCollision(c.asts)
20+
if !slices.Equal(res, c.out) {
21+
t.Errorf("asteroidCollision(%v)=%v but expected %v", c.asts, res, c.out)
22+
}
23+
}
724
}
Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
# asteroid_collision
1+
# 735. Asteroid Collision
22

33
## Problem
4+
We are given an array asteroids of integers representing asteroids in a row. The indices of the asteroid in the array represent their relative position in space.
45

5-
Paste the problem description here.
6+
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.
7+
8+
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.
69

710
## Examples
811

12+
Example 1:
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+
Input: asteroids = [8,-8]
19+
Output: []
20+
Explanation: The 8 and -8 collide exploding each other.
21+
22+
Example 3:
23+
Input: asteroids = [10,2,-5]
24+
Output: [10]
25+
Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
26+
27+
Example 4:
28+
Input: asteroids = [3,5,-6,2,-1,4]​​​​​​​
29+
Output: [-6,2,4]
30+
Explanation: The asteroid -6 makes the asteroid 3 and 5 explode, and then continues going left. On the other side, the asteroid 2 makes the asteroid -1 explode and then continues going right, without reaching asteroid 4.
31+
932
## Notes
1033
- Constraints:
11-
- Edge cases:
12-
- Complexity:
34+
2 <= asteroids.length <= 104
35+
-1000 <= asteroids[i] <= 1000
36+
asteroids[i] != 0

0 commit comments

Comments
 (0)