mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 01:49:41 +01:00
34 lines
573 B
Go
34 lines
573 B
Go
|
package main
|
||
|
|
||
|
import as "github.com/emirpasic/gods/v2/stacks/arraystack"
|
||
|
|
||
|
func asteroidCollision(asteroids []int) []int {
|
||
|
st := as.New[int]()
|
||
|
|
||
|
for _, asteroid := range asteroids {
|
||
|
alive := true
|
||
|
|
||
|
for top, ok := st.Peek(); ok && asteroid < 0 && top > 0; top, ok = st.Peek() {
|
||
|
if top < -asteroid {
|
||
|
st.Pop()
|
||
|
continue
|
||
|
} else if top == -asteroid {
|
||
|
st.Pop()
|
||
|
}
|
||
|
|
||
|
alive = false
|
||
|
break
|
||
|
}
|
||
|
|
||
|
if alive {
|
||
|
st.Push(asteroid)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
result := make([]int, st.Size())
|
||
|
for i := st.Size() - 1; i >= 0; i-- {
|
||
|
result[i], _ = st.Pop()
|
||
|
}
|
||
|
return result
|
||
|
}
|