-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_469_NextGreaterElementI.cpp
160 lines (117 loc) · 3.92 KB
/
LC_469_NextGreaterElementI.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
https://leetcode.com/problems/next-greater-element-i/
496. Next Greater Element I
*/
/***************** GFG ************************/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to find the next greater element for each element of the array.
vector<long long> nextLargerElement(vector<long long> arr, int n){
// Your code here
vector<long long> ans(n,0);
stack<long long> st;
st.push(-1);
for(int i =n-1; i >=0; i--)
{
while( (st.top() != -1) && (st.top() <= arr[i]) )
st.pop();
ans[i] = st.top();
st.push(arr[i]);
}
return ans;
}//end
};
// { Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<long long> arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
Solution obj;
vector <long long> res = obj.nextLargerElement(arr, n);
for (long long i : res) cout << i << " ";
cout<<endl;
}
return 0;
} // } Driver Code Ends
/***************** LC ************************/
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int> ans;
stack<int> st;
unordered_map<int,int> umap;
st.push(-1);
for(int i = nums2.size()-1; i>=0; i--) // from last element to first
{
while(st.top() != -1 && st.top() <= nums2[i])
st.pop();
umap[nums2[i]] = st.top();
st.push(nums2[i]);
}
for(int i: nums1)
ans.push_back(umap[i]);
return ans;
}//end
// vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
// vector<int> ans;
// stack<int> st;
// unordered_map<int,int> umap;
// for(int i = nums2.size()-1; i>=0; i--) // from last element to first
// {
// if(st.empty()) umap[nums2[i]] = -1; // if stack is empty, that is no greater element exist till now
// // if stack is not empty
// else if(st.top() > nums2[i]) // if stack top is greater than current element, that is we have next greater element of curr element
// umap[nums2[i]] = st.top();
// else if(st.top() <= nums2[i])
// {
// while(!st.empty() && st.top() <= nums2[i])
// st.pop();
// if(st.empty()) umap[nums2[i]] = -1;
// else umap[nums2[i]] = st.top();
// }
// st.push(nums2[i]);
// }
// for(int i: nums1)
// ans.push_back(umap[i]);
// return ans;
// }//end
// vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
// vector<int> ans;
// bool flageq = false, flagpush = false;
// for(int i=0; i < nums1.size(); i++)
// {
// flageq = false;
// flagpush = false;
// for(int j=0; j<nums2.size(); j++)
// {
// if(flageq)
// {
// if(nums1[i] < nums2[j])
// {
// ans.push_back(nums2[j]);
// flagpush=true;
// break;
// }
// }
// if(nums1[i] == nums2[j])
// {
// flageq = true;
// }
// } //for j
// if(!flagpush) ans.push_back(-1); // for last case
// }// for i
// return ans;
// }//end
};