C++ easy-to-understand code || monotonic stack and priority queue || O(nlogn) time || O(n) space
\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> res(n, -1);\n        s
```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> res(n, -1);\n        stack<int> first;\n        auto comp = [&nums](int i, int j) { return nums[i] > nums[j]; };\n        priority_queue<int, vector<int>, decltype(comp)> second(comp);\n        for (int i = 0; i < n; i++) {\n            while (!second.empty()) {\n                if (nums[i] <= nums[second.top()]) break;\n                // find second greater\n                res[second.top()] = nums[i];\n                second.pop();\n            }\n            while (!first.empty()) {\n                if (nums[i] <= nums[first.top()]) break;\n                // find first greater, push it into the second container(priority_queue)\n                second.push(first.top());\n                first.pop();\n            }\n            first.push(i);\n        }\n        return res;\n    }\n};\n```
['Heap (Priority Queue)', 'Monotonic Stack']