Skip to content

Commit

Permalink
Create 0735-asteroid-collision.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
a93a authored Jul 20, 2023
1 parent f474f61 commit d7ecfad
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions kotlin/0735-asteroid-collision.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
fun asteroidCollision(ast: IntArray): IntArray {
val stack = LinkedList<Int>()

for (_a in ast) {
var a = _a
while (stack.isNotEmpty() && a < 0 && stack.peekLast() > 0) {
val diff = a + stack.peekLast()
if (diff < 0) {
stack.removeLast()
} else if (diff > 0) {
a = 0
} else {
a = 0
stack.removeLast()
}
}

if (a != 0) stack.addLast(a)
}

return stack.toIntArray()
}
}

0 comments on commit d7ecfad

Please sign in to comment.