-
Notifications
You must be signed in to change notification settings - Fork 0
/
next greater element 1.cpp
43 lines (37 loc) · 1.23 KB
/
next greater element 1.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution
{
public:
vector<int> nextGreaterElement(vector<int> &nums1, vector<int> &nums2)
{
int n = nums2.size();
stack<int> st;
map<int, int> m;
// Iterate through nums2 from right to left
for (int i = n - 1; i >= 0; i--)
{
// Pop elements from the stack until it's empty or we find an element greater than the current element
while (!st.empty() && st.top() < nums2[i])
st.pop();
// Store the next greater element in the map
if (!st.empty())
{
m[nums2[i]] = st.top();
}
else
{
m[nums2[i]] = -1;
}
// Push the current element into the stack
st.push(nums2[i]);
}
// Create a vector to store the result
vector<int> ans(nums1.size(), -1);
// Iterate through nums1 to get the next greater element for each element
for (int i = 0; i < nums1.size(); i++)
{
ans[i] = m[nums1[i]];
}
return ans;
}
};
// leetcode - 496