-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextGreater.java
More file actions
29 lines (27 loc) · 806 Bytes
/
NextGreater.java
File metadata and controls
29 lines (27 loc) · 806 Bytes
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
package Stack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
/**
* @author Vishal Singh
*/
public class NextGreater {
public static void main(String[] args) {
int[] arr = {5,15,10,8,6,12,9,18};
// {15,18,12,12,12,18,18,-1}
ArrayList<Integer> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
list.add(-1);
stack.push(arr[arr.length-1]);
for (int i = arr.length-2; i >=0; i--) {
while (!stack.isEmpty() && stack.peek()<= arr[i]){
stack.pop();
}
int ng = stack.empty()?-1: stack.peek();
list.add(ng);
stack.push(arr[i]);
}
Collections.reverse(list);
System.out.println(list);
}
}