Skip to content

Commit 0af5480

Browse files
authored
Create Solution.java
1 parent cc34905 commit 0af5480

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)