-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNextGreaterElement2.java
More file actions
40 lines (30 loc) · 1.46 KB
/
Copy pathNextGreaterElement2.java
File metadata and controls
40 lines (30 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Given a circular array (the next element of the last element is the first
// element of the array), print the Next Greater Number for every element.
// The Next Greater Number of a number x is the first greater number to its
// traversing-order next in the array, which means you could search circularly
// to find its next greater number. If it doesn't exist, output -1 for this number.
// See: https://leetcode.com/problems/next-greater-element-ii/
package leetcode.stack;
import java.util.Arrays;
import java.util.Stack;
public class NextGreaterElement2 {
public int[] nextGreaterElements(int[] nums) {
int n = nums.length;
int[] ans = new int[nums.length];
for (int i = 0; i < ans.length; i++)
ans[i] = -1;
Stack<int[]> stack = new Stack<>();
for (int i = 0; i < 2*n; i++) {
while (!stack.empty() && nums[i % n] > stack.peek()[0])
ans[stack.pop()[1]] = nums[i % n];
stack.add(new int[] { nums[i % n], i % n });
}
return ans;
}
public static void main(String[] args) {
NextGreaterElement2 sln = new NextGreaterElement2();
System.out.println(Arrays.toString(sln.nextGreaterElements(new int[] { 5, 4, 3, 2, 1 })));
System.out.println(Arrays.toString(sln.nextGreaterElements(new int[] { 5, 4, 3, 2, 1 })));
System.out.println(Arrays.toString(sln.nextGreaterElements(new int[] { 1, 2, 3, 2, 1 })));
}
}