Skip to content

Commit d7ecfad

Browse files
authored
Create 0735-asteroid-collision.kt
1 parent f474f61 commit d7ecfad

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Diff for: kotlin/0735-asteroid-collision.kt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
fun asteroidCollision(ast: IntArray): IntArray {
3+
val stack = LinkedList<Int>()
4+
5+
for (_a in ast) {
6+
var a = _a
7+
while (stack.isNotEmpty() && a < 0 && stack.peekLast() > 0) {
8+
val diff = a + stack.peekLast()
9+
if (diff < 0) {
10+
stack.removeLast()
11+
} else if (diff > 0) {
12+
a = 0
13+
} else {
14+
a = 0
15+
stack.removeLast()
16+
}
17+
}
18+
19+
if (a != 0) stack.addLast(a)
20+
}
21+
22+
return stack.toIntArray()
23+
}
24+
}

0 commit comments

Comments
 (0)