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 cc34905 commit 0af5480Copy full SHA for 0af5480
solution/0503.Next Greater Element II/Solution.java
@@ -0,0 +1,21 @@
1
+class Solution {
2
+ public int[] nextGreaterElements(int[] nums) {
3
+ int n = nums.length;
4
+ int len = (n << 1) - 1;
5
+ int[] res = new int[n];
6
+ Deque<Integer> stack = new ArrayDeque<>();
7
+ for (int i = 0; i < len; ++i) {
8
+ int x = nums[i < n ? i : i - n];
9
+ while (!stack.isEmpty() && x > nums[stack.peek()]) {
10
+ res[stack.pop()] = x;
11
+ }
12
+ if (i < n) {
13
+ stack.push(i);
14
15
16
+ while (!stack.isEmpty()) {
17
+ res[stack.pop()] = -1;
18
19
+ return res;
20
21
+}
0 commit comments