-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path496.cpp
35 lines (34 loc) · 925 Bytes
/
496.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
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int>V;
if(nums1.empty() && nums2.empty()){
return V;
}
stack<int>S;
S.push(nums2[0]);
map<int,int>mp;
for(int i=0;i<nums2.size();i++){
if(nums2[i]>S.top()){
while(!S.empty() && nums2[i]>S.top()){
mp[S.top()]=nums2[i];
S.pop();
}
S.push(nums2[i]);
}
else{
S.push(nums2[i]);
}
}
map<int,int>:: iterator it;
for(int j=0;j<nums1.size();j++){
if(mp.find(nums1[j])==mp.end()){
V.push_back(-1);
}
else {
V.push_back(mp.find(nums1[j])->second);
}
}
return V;
}
};