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

g2401_2500.s2454_next_greater_element_iv.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g2401_2500.s2454_next_greater_element_iv

// #Hard #Array #Sorting #Binary_Search #Stack #Heap_Priority_Queue #Monotonic_Stack
// #2023_07_04_Time_749_ms_(100.00%)_Space_52.9_MB_(100.00%)

import java.util.ArrayDeque
import java.util.Deque

class Solution {
    fun secondGreaterElement(nums: IntArray): IntArray {
        val res = IntArray(nums.size)
        res.fill(-1)
        val stack1: Deque = ArrayDeque()
        val stack2: Deque = ArrayDeque()
        val tmp: Deque = ArrayDeque()
        for (i in nums.indices) {
            while (stack2.isNotEmpty() && nums[i] > nums[stack2.peek()]) {
                res[stack2.pop()] = nums[i]
            }
            while (stack1.isNotEmpty() && nums[i] > nums[stack1.peek()]) {
                tmp.push(stack1.pop())
            }
            while (tmp.isNotEmpty()) {
                stack2.push(tmp.pop())
            }
            stack1.push(i)
        }
        return res
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy