Next Greater Element II LeetCode solution in Java

1
class Solution {
2
public int[] nextGreaterElements(int[] nums) {
3
Stack<Integer> s = new Stack<>();
4
for (int i = nums.length - 1; i >= 0; i--) {
5
s.push(nums[i]);
6
}
7
for (int i = nums.length - 1; i >= 0; i--) {
8
int num = nums[i];
9
while (!s.isEmpty() && s.peek() <= nums[i]) {
10
s.pop();
11
}
12
nums[i] = s.empty() ? -1 : s.peek();
13
s.push(num);
14
}
15
return nums;
16
}
17
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0