1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-09 15:59:06 +01:00

go: add «735. Asteroid Collision»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-08-12 23:43:50 +02:00
parent 23cd6ac490
commit dca74915b4
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

33
go/asteroid-collision.go Normal file
View file

@ -0,0 +1,33 @@
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
}