-
Notifications
You must be signed in to change notification settings - Fork 0
/
496.hpp
53 lines (42 loc) · 1.17 KB
/
496.hpp
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Created by bai on 17-6-25.
//
#ifndef LEETCODE_496_HPP
#define LEETCODE_496_HPP
#include <iostream>
#include <queue>
#include <algorithm>
#include <unordered_map>
#include <stack>
using namespace std;
/**
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s
elements are subset of nums2.
Find all the next greater numbers for nums1's elements in the corresponding places of nums2.
The Next Greater Number of a number x in nums1 is
the first greater number to its right in nums2. If it does not exist, output -1 for this number.
*/
class Solution {
public:
vector<int> nextGreaterElement(vector<int> &findNums, vector<int> &nums) {
stack<int> q;
unordered_map<int, int> m;
for (int n : nums) {
while (q.size() > 0 && n > q.top()) {
m[q.top()] = n;
q.pop();
}
q.emplace(n);
}
vector<int> res;
for (int n : findNums) {
if (m.count(n) > 0) {
res.emplace_back(m[n]);
} else {
res.emplace_back(-1);
}
}
return res;
}
};
#endif //LEETCODE_496_HPP