-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path503.cpp
More file actions
32 lines (32 loc) · 782 Bytes
/
503.cpp
File metadata and controls
32 lines (32 loc) · 782 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
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
if(n==0)
return {};
if(n==1)
return {-1};
vector<int> res(n,-1);
stack<int> st;
int j = n-1;
int maxval = INT_MIN;
for(int i=0; i<n; i++){
if(nums[i]>maxval){
maxval = nums[i];
j = i;
}
}
int i = (j+1)%n;
st.push(i);
i = (i+1)%n;
for(; i!=(j+1)%n; i = (i+1)%n){
// cout<< i <<"\n";
while(!st.empty() && nums[st.top()]<nums[i]){
res[st.top()] = nums[i];
st.pop();
}
st.push(i);
}
return res;
}
};