Skip to content

Commit

Permalink
add sol
Browse files Browse the repository at this point in the history
  • Loading branch information
ductnn committed Dec 13, 2023
1 parent f188183 commit f2745f2
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
)

func asteroidCollision(asteroids []int) []int {
result := []int{}
for _, v := range asteroids {
if v > 0 {
result = append(result, v)
} else {
for len(result) > 0 && result[len(result)-1] > 0 && result[len(result)-1] < -v {
result = result[:len(result)-1]
}
if len(result) > 0 && result[len(result)-1] == -v {
result = result[:len(result)-1]
} else if len(result) == 0 || result[len(result)-1] < 0 {
result = append(result, v)
}
}
}
return result
}

func main() {
asteroids := []int{5, 10, -5}
fmt.Println(asteroidCollision(asteroids))
}

0 comments on commit f2745f2

Please sign in to comment.