All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0735_asteroid_collision.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.30
Show newest version
package g0701_0800.s0735_asteroid_collision

// #Medium #Array #Stack #Level_2_Day_18_Stack
// #2023_03_02_Time_243_ms_(100.00%)_Space_37.9_MB_(88.46%)

import java.util.Deque
import java.util.LinkedList

class Solution {
    fun asteroidCollision(asteroids: IntArray): IntArray {
        val stack: Deque = LinkedList()
        for (a in asteroids) {
            if (a > 0) {
                stack.addLast(a)
            } else {
                if (stack.isNotEmpty() && stack.peekLast() > 0) {
                    if (stack.peekLast() == Math.abs(a)) {
                        stack.pollLast()
                    } else {
                        while (stack.isNotEmpty() && stack.peekLast() > 0 && stack.peekLast() < Math.abs(a)) {
                            stack.pollLast()
                        }
                        if (stack.isNotEmpty() && stack.peekLast() > 0 && stack.peekLast() == Math.abs(a)) {
                            stack.pollLast()
                        } else if (stack.isEmpty() || stack.peekLast() < 0) {
                            stack.addLast(a)
                        }
                    }
                } else {
                    stack.addLast(a)
                }
            }
        }
        val ans = IntArray(stack.size)
        for (i in stack.indices.reversed()) {
            ans[i] = stack.pollLast()
        }
        return ans
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy