Stack, BinarySearch, Heap Solutions Python
Stack Approach\nTime: O(n)\nSpace: O(n)\n\ndef secondGreaterElement(self, nums: List[int]) -> List[int]:\n    n = len(nums)\n    ans = [-1]*n\n\n    stack = []\
**Stack Approach**\n**Time: O(n)\nSpace: O(n)**\n```\ndef secondGreaterElement(self, nums: List[int]) -> List[int]:\n    n = len(nums)\n    ans = [-1]*n\n\n    stack = []\n    next_stack = []\n    for i in range(n):\n\n        while next_stack and nums[next_stack[-1]] < nums[i]:\n            ans[next_stack.pop()] = nums[i]\n\n        indices = []\n        while stack and nums[stack[-1]] < nums[i]:\n            indices += [stack.pop()]\n        next_stack += indices[::-1]\n        stack.append(i)\n    \n    return ans\n```\n\n**Binary Search Approach**\n**Time: O(nlogn)\nSpace: O(n)**\n```\ndef secondGreaterElement(self, nums: List[int]) -> List[int]:\n    n = len(nums)\n    ans = [-1]*n\n\n    def BS(i):\n        L = 0; R = len(stack)\n        while L < R:\n            M = (L+R)//2\n            if nums[stack[M]] >= nums[i]:\n                L = M+1\n            else:\n                R = M\n        return L\n\n    stack = []\n    next_stack = []\n    for i in range(n):\n\n        while next_stack and nums[next_stack[-1]] < nums[i]:\n            ans[next_stack.pop()] = nums[i]\n\n        idx = BS(i)\n        if idx < len(stack):\n            next_stack += stack[idx:]\n            stack[idx:] = []\n        stack.append(i)\n    \n    return ans\n```\n\n**Heap Approach**\n**Time: O(nlogn)\nSpace: O(n)**\n```\ndef secondGreaterElement(self, nums: List[int]) -> List[int]:\n    n = len(nums)\n    ans = [-1]*n\n\n    stack = []\n    min_heap = []\n    for i in range(n):\n\n        while min_heap and min_heap[0][0] < nums[i]:\n            _, idx = heappop(min_heap)\n            ans[idx] = nums[i]\n\n        while stack and nums[stack[-1]] < nums[i]:\n            j = stack.pop()\n            heappush(min_heap, [nums[j], j])\n\n        stack.append(i)\n    \n    return ans\n```\n\n\n
['Binary Search', 'Stack', 'Heap (Priority Queue)', 'Python']