Python heap
I created a min heap, to which tuples containing (nums[i], i) would be added. Also, use a dictionary to record the number of prior greater values we have found 
I created a min heap, to which tuples containing (nums[i], i) would be added. Also, use a dictionary to record the number of prior greater values we have found for each tuple. We need to iterate through the values in nums, each time checking whether the value is greater than the first element of the tuple at the top of the min heap. As long as the value is greater, we then update the dictionary value, and if this is the second time we have found a greater value, we put the value in the result list, res. If it is not the second time, we do need to pop the item from the heap, and then put it back on the heap after having checked the remaining qualifying values in the heap.\n\n```\ndef secondGreaterElement(self, nums: List[int]) -> List[int]:\n        res = [-1 for i in range(len(nums))]\n        d = {}\n        for i in range(len(nums)):\n            d[(nums[i], i)] = 0\n        from heapq import heappush, heappop\n        h = [] # add the number, plus its index, as tuple\n        for i in range(len(nums)):\n            addback = []\n            while h:\n                if nums[i] > h[0][0]:\n                    if d[(h[0][0], h[0][1])] == 0:\n                        d[(h[0][0], h[0][1])] += 1\n                        addback.append([h[0][0], h[0][1]])\n                        heappop(h)\n                    elif d[(h[0][0], h[0][1])] == 1:\n                        res[h[0][1]] = nums[i]\n                        heappop(h)\n                else:\n                    break\n            heappush(h, [nums[i], i])\n            for k,l in addback:\n                heappush(h, [k,l])\n        return res\n                ```
['Heap (Priority Queue)']