Skip to content

735. Asteroid Collision #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ continually updating 😃.

### Stack
* [155. Min Stack](src/0155_min_stack/min_stack.go)
* [735. Asteroid Collision](src/0735_asteroid_collision/ac.go)

### String
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*
Expand Down
46 changes: 46 additions & 0 deletions src/0735_asteroid_collision/ac.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
735. Asteroid Collision
https://leetcode.com/problems/asteroid-collision/

We are given an array asteroids of integers representing asteroids in a row.

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.

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.

Note:
The length of asteroids will be at most 10000.
Each asteroid will be a non-zero integer in the range [-1000, 1000]..
*/
// time: 2019-01-14

package ac

// stack
// time complexity: O(N), where NN is the number of asteroids. Our stack pushes and pops each asteroid at most once.
// space complexity: O(N), the size of stack.
func asteroidCollision(asteroids []int) []int {
stack := make([]int, 0)

for _, asteroid := range asteroids {
flag := true
for len(stack) > 0 && asteroid < 0 && stack[len(stack)-1] > 0 {
if stack[len(stack)-1] == -asteroid {
stack = stack[:len(stack)-1]
} else if stack[len(stack)-1] < -asteroid {
stack = stack[:len(stack)-1]
continue
}
flag = false
break
}
if flag {
stack = append(stack, asteroid)
}
}
return stack
}
30 changes: 30 additions & 0 deletions src/0735_asteroid_collision/ac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ac

import (
"reflect"
"testing"
)

func TestAsteroidCollision(t *testing.T) {
testCases := [][]int{
{5, 10, -5},
{-2, 2, -1, -2},
{-2, -1, 1, 2},
{8, -8},
{10, 2, -5},
}

expected := [][]int{
{5, 10},
{-2},
{-2, -1, 1, 2},
{},
{10},
}

for index, asteroids := range testCases {
if res := asteroidCollision(asteroids); !reflect.DeepEqual(res, expected[index]) {
t.Errorf("expected %v, got %v", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
|0735|[735. Asteroid Collision](0735_asteroid_collision/ac.go)|Medium|*`stack`*|
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|