Similar to Next Greater Element II using stack and priority queue | Faster | NlogN
Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly solve the next greater element using decreasing stack.\nfrom left to right\n\n#
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly solve the next greater element using decreasing stack.\nfrom left to right\n\n# Approach\nWe are storing elements in decreasing order in stack if new element is greater than the top element of stack we are removing it from the stack and inserting it in the priority queue which means that these element have one greater element.\nBefore doing the above operation we check the element which have one greater element present in priority queue is greater than current element than it is the second greater element of that index so we will mark it and remove it from priority_queue.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> \n\n- Space complexity: O(2n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        //jai shri ram\n        stack<int>st1;\n        using pi=pair<int,int>;\n        priority_queue<pi,vector<pi>,greater<pi>>st2;\n        int n=nums.size();\n        vector<int>ans(n,-1);\n        st1.push(0);\n        for(int i=1;i<n;i++){\n            while(st2.size()>0 && st2.top().first<nums[i]){\n                ans[st2.top().second]=nums[i];\n                st2.pop();\n            }\n            while(st1.size()>0 && nums[st1.top()]<nums[i]){\n                st2.push({nums[st1.top()],st1.top()});\n                st1.pop();\n            }\n            st1.push(i);\n        }\n        return ans;\n    }\n};\n```
['Stack', 'Heap (Priority Queue)', 'Monotonic Stack', 'C++']