题解 | #大雨吃小鱼#
大雨吃小鱼
https://www.nowcoder.com/practice/77199defc4b74b24b8ebf6244e1793de
import java.util.Scanner;
import java.util.*;
import java.lang.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
// while (in.hasNextInt()) { // 注意 while 处理多个 case
// int a = in.nextInt();
// int b = in.nextInt();
// System.out.println(a + b);
// }
int n = in.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i ++){
arr[i] = in.nextInt();
}
int[] times = new int[n];
Deque<Integer> stack = new ArrayDeque<>();
for (int i = n - 1; i >= 0; i --){
while (!stack.isEmpty() && arr[stack.peek()] < arr[i]){
int cur = stack.pop();
times[i] = Math.max(times[i] + 1, times[cur]);
}
stack.push(i);
}
// if (stack.isEmpty()){
// return 0;
// }
int res = 0;
for (int i : times){
res = Math.max(res, i);
}
System.out.println(res);
}
}
