Update 单调栈.md

add comments
This commit is contained in:
Rui Yang
2020-11-10 23:16:02 -06:00
committed by GitHub
parent d0f78c9b7a
commit 91e56f8727

View File

@ -188,8 +188,10 @@ class Solution {
Stack<Integer> stack = new Stack<>();
int[] ans = new int[T.length];
for (int i = 0; i < T.length; i++) {
// 如果压栈之后不满足单调递减弹出元素直至保持单调性
while (!stack.isEmpty() && T[i] > T[stack.peek()]) {
int index = stack.pop();
// 被弹出的元素T[index])都是小于当前的元素(T[i])由于栈内元素单调递减大于被弹出元素index的最近的就是当前元素(i)
ans[index] = i - index;
}
stack.push(i);