forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0496.cpp
More file actions
28 lines (27 loc) · 709 Bytes
/
0496.cpp
File metadata and controls
28 lines (27 loc) · 709 Bytes
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
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums)
{
unordered_map<int, int> dic;
vector<int> stack;
for (int n : nums)
{
while (!stack.empty() and stack.back() < n)
{
dic[stack.back()] = n; stack.pop_back();
}
stack.push_back(n);
}
vector<int> res;
for (int n : findNums)
{
if (dic.count(n)) res.push_back(dic[n]);
else res.push_back(-1);
}
return res;
}
};