We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 139e8e9 commit 6fac47cCopy full SHA for 6fac47c
next_greater_element_ii.cpp
@@ -0,0 +1,22 @@
1
+class Solution {
2
+public:
3
+ vector<int> nextGreaterElements(vector<int>& nums) {
4
+
5
+ int n = nums.size();
6
+ if(n == 0) return {};
7
8
+ vector<int> result(n, -1);
9
+ stack<int> st;
10
11
+ for(int i=2*n-1; i>=0; i--) {
12
+ int num = nums[i % n];
13
+ while(!st.empty() && nums[st.top()] <= num) st.pop();
14
+ if(st.empty()) result[i % n] = -1;
15
+ else result[i % n] = nums[st.top()];
16
17
+ st.push(i % n);
18
+ }
19
+ return result;
20
21
22
+};
0 commit comments