g0501_0600.s0503_next_greater_element_ii.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0501_0600.s0503_next_greater_element_ii;
// #Medium #Array #Stack #Monotonic_Stack
import java.util.ArrayDeque;
import java.util.Deque;
public class Solution {
public int[] nextGreaterElements(int[] nums) {
int[] result = new int[nums.length];
Deque stack = new ArrayDeque<>();
for (int i = nums.length * 2 - 1; i >= 0; i--) {
while (!stack.isEmpty() && nums[stack.peek()] <= nums[i % nums.length]) {
stack.pop();
}
result[i % nums.length] = stack.isEmpty() ? -1 : nums[stack.peek()];
stack.push(i % nums.length);
}
return result;
}
}