C++ || Stack + Priority Queue || Easy to Understand Solution
\nclass Solution {\npublic:\n    \n    struct comp{  \n        bool operator()(const pair<int,int>&x,const pair<int,int>&y){\n            return x.first > y.fir
```\nclass Solution {\npublic:\n    \n    struct comp{  \n        bool operator()(const pair<int,int>&x,const pair<int,int>&y){\n            return x.first > y.first;\n        }\n    };\n    \n    vector<int> secondGreaterElement(vector<int>& nums) {\n        \n        int n = nums.size();\n        \n        vector<int>next(n,-1);\n        \n        stack<int>S;\n        \n        for(int i=n-1;i>=0;i--){\n            while(!S.empty() && nums[i]>=nums[S.top()]){\n                S.pop();\n            }\n            if(!S.empty()){\n                next[i]=S.top();\n            }\n            S.push(i);\n        }\n        \n        vector<vector<pair<int,int>>>index(n);\n        \n        for(int i=0;i<n;i++){\n            if(next[i]==-1) continue;\n            int j=next[i];\n            index[j].push_back({nums[i],i});\n        }\n        \n        priority_queue<pair<int,int>,vector<pair<int,int>>,comp>pq;\n        \n        vector<int>ans(n,-1);\n        \n        for(int i=0;i<n;i++){\n            while(!pq.empty() && pq.top().first < nums[i]){\n                ans[pq.top().second] = nums[i];\n                pq.pop();\n            }\n            for(auto &ele:index[i]){\n                pq.push({ele.first,ele.second});\n            }\n        }\n        \n        return ans;\n        \n    }\n};\n```
['Stack', 'C', 'Heap (Priority Queue)']