Two PriorityQueue
Complexity\n\n# Code\n\nclass Solution {\n    fun secondGreaterElement(nums: IntArray): IntArray {\n        val len = nums.size\n        var rez = IntArray(len)
# Complexity\n![image.png](https://assets.leetcode.com/users/images/a8bcfb0e-172a-49c1-85c9-62d6be0c991a_1718275908.948556.png)\n# Code\n```\nclass Solution {\n    fun secondGreaterElement(nums: IntArray): IntArray {\n        val len = nums.size\n        var rez = IntArray(len){-1}\n        val stack1 =  PriorityQueue<Pair<Int,Int>> { a,b -> a.second-b.second}\n        val stack2 =  PriorityQueue<Pair<Int,Int>> { a,b -> a.second-b.second}\n        for (i in 0 until len) {\n            // println("-stack1=${stack1.toList()},stack2=${stack2.toList()}")\n            while (stack2.isNotEmpty()) {\n                val head = stack2.peek()\n                if (nums[i]>head.second) {\n                    rez[head.first]=nums[i]\n                    stack2.poll()\n                }\n                else break\n            }\n            while (stack1.isNotEmpty()) {\n                val head = stack1.peek()\n                if (nums[i]>head.second) {\n                    stack2.offer(Pair(head.first,head.second))\n                    stack1.poll()\n                }\n                else break\n            }\n            stack1.offer(Pair(i,nums[i]))\n            // println("+stack1=${stack1.toList()},stack2=${stack2.toList()}")\n        }\n        return rez\n    }\n}\n```
['Kotlin']