- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathnge1_496.cpp
25 lines (25 loc) · 699 Bytes
/
nge1_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
classSolution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size(), m = nums2.size();
vector<int> res(n, -1);
stack<int> st;
unordered_map<int, int> mp;
for(int i = 0; i < m; i++){
int val = nums2[i];
while(!st.empty() and val > st.top()){
mp[st.top()] = val;
st.pop();
}
st.push(val);
}
for(int i = 0; i < n; i++){
int val = nums1[i];
if(mp.find(val) != mp.end()){
int nge = mp[val];
res[i] = nge;
}
}
return res;
}
};