-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path496.cpp
More file actions
22 lines (22 loc) · 685 Bytes
/
Copy path496.cpp
File metadata and controls
22 lines (22 loc) · 685 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> nextGreaterElement(vector<int> &findNums, vector<int> &nums) {
stack<int> key;
unordered_map<int, int> map;
vector<int> ret;
for (int i = 0; i < nums.size(); ++i) {
while (!key.empty() && nums[i] > key.top()) {
map.insert(pair<int, int>(key.top(), nums[i]));
key.pop();
}
key.push(nums[i]);
}
for (int i = 0; i < findNums.size(); ++i) {
if (map.find(findNums[i]) != map.end())
ret.push_back(map[findNums[i]]);
else
ret.push_back(-1);
}
return ret;
}
};