mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 12:04:37 +08:00
Update 单调栈.md
add comments
This commit is contained in:
@ -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);
|
||||
|
Reference in New Issue
Block a user