-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21-StackQueueImplent.cpp
More file actions
45 lines (36 loc) · 1.36 KB
/
Copy path21-StackQueueImplent.cpp
File metadata and controls
45 lines (36 loc) · 1.36 KB
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
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <queue>
using namespace std;
//Next Greater Element (NGE) is the element which is greater than the current element and is present on the right side of the current element in the array. If there is no greater element on the right side, then the next greater element for the current element is -1.
vector<int> nse(vector<int>& arr) {
int n = arr.size();
vector<int> ans(n);
stack<int> st;
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && arr[st.top()] >= arr[i])
st.pop();
ans[i] = st.empty() ? n : st.top();
st.push(i);
}
return ans;
}
//Previous Greater Element (PGE) is the element which is greater than the current element and is present on the left side of the current element in the array. If there is no greater element on the left side, then the previous greater element for the current element is -1.
vector<int> pse(vector<int>& arr) {
int n = arr.size();
vector<int> ans(n);
stack<int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && arr[st.top()] >= arr[i])
st.pop();
ans[i] = st.empty() ? -1 : st.top();
st.push(i);
}
return ans;
}
int main()
{
return 0;
}