-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
43 lines (39 loc) · 929 Bytes
/
main.cpp
File metadata and controls
43 lines (39 loc) · 929 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
std::vector<int> nextGreaterElement(std::vector<int>& findNums, std::vector<int>& nums) {
if (findNums.empty()) {
return findNums;
}
std::unordered_map<int, int> dict;
std::stack<int> st;
int prev = nums[0];
for (int i: nums) {
if (i > prev) {
while (!st.empty()) {
int temp = st.top();
if (i > temp) {
dict[temp] = i;
st.pop();
} else {
break;
}
}
}
st.push(i);
prev = i;
}
std::vector<int> result;
for (int i: findNums) {
if (dict.count(i) > 0) {
result.push_back(dict[i]);
} else {
result.push_back(-1);
}
}
return result;
}
int main() {
return 0;
}