AcWing 830. 单调栈-java
原题链接
简单
作者:
Susu
,
2020-01-27 22:58:24
,
所有人可见
,
阅读 1370
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
StringBuffer sb = new StringBuffer();
Stack<Integer> stack = new Stack<>();
for (int val : arr) {
while (!stack.isEmpty() && val <= stack.peek()) {
stack.pop();
}
if (stack.isEmpty()) {
sb.append(-1).append(" ");
}else {
sb.append(stack.peek()).append(" ");
}
stack.push(val);
}
System.out.println(sb.toString().trim());
}
}