-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP503.cpp
36 lines (34 loc) · 985 Bytes
/
P503.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "header.h"
#include <algorithm>
#include <queue>
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> ret(nums.size(), -1);
if (nums.empty()) return ret;
// priority_queue<pair<int,int>, vector<pair<int, int>>, greater<pair<int,int>> > q;
stack<pair<int,int>> q;
q.emplace(nums[0],0);
for (int i=1; i<nums.size();i++){
while (!q.empty() && nums[i] > q.top().first) {
ret[q.top().second] = nums[i];
q.pop();
}
q.emplace(nums[i], i);
}
for (int i=0; i<nums.size(); i++) {
while (!q.empty() && nums[i] > q.top().first) {
ret[q.top().second] = nums[i];
q.pop();
}
}
return ret;
}
};
int main() {
vector<int> a;
a = {2,2,3,3,4,5,6,7,1};
// a = {5,4,3,2,1};
print(Solution().nextGreaterElements(a));
return 0;
}