[C++] 2 layers (stack + pq) | O(n logn)
When the first greater is found for some elements in the monotonic stack, transfer them into the priority queue\nin order to wait for the second greater:\n\n us
When the first greater is found for some elements in the monotonic stack, transfer them into the priority queue\nin order to wait for the second greater:\n\n* use a <b>monotonic stack</b> to keep the decreasing sequences (and detect the <b>first greater</b> element for those\nelements in stack that are smaller than the current <b>nums[i]</b>)\n* priority queue to find the <b>second greater</b> for the elements that are transferred from the monotonic stack in the pq, after finding the first greater\n\n```\nclass Solution {\npublic:\n    vector<int> secondGreaterElement(vector<int>& nums) {\n        vector<int> ans(nums.size(), -1);\n        \n        // q1 -> keep track of the first greater\n        deque<int> q1;\n\t\t\n\t\t// q2 -> keep track of the second greater\n        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q2;\n        \n        int n = nums.size();\n        for (int i = 0; i < n; ++i) {\n            while (!q2.empty() && q2.top().first < nums[i]) {\n                ans[q2.top().second] = nums[i];\n                q2.pop();\n            }\n    \n            while (!q1.empty() && nums[q1.back()] < nums[i]) {\n                q2.push({nums[q1.back()], q1.back()});\n                q1.pop_back();\n            }\n            \n            q1.push_back(i);\n        }\n        \n        return ans;\n    }\n};\n```
['C', 'Heap (Priority Queue)', 'Monotonic Stack']