单调栈

单调栈: 单调性(单调递增或单调递减)的栈。

1
2
3
4
5
6
7
8
9
10
11
int[] arrays = new int[]{73, 74, 75, 71, 69, 72, 76, 73};
Stack<Integer> s = new Stack<>();
for (int i = 0; i < arrays.length; i++) {
while (!s.isEmpty() && s.peek() > arrays[i]) {
s.pop();
}
s.push(arrays[i]);
}
System.out.println(s);

output: [69, 72, 73]
Look at your mood.