单调栈
单调栈通用模版:
var monotonicStack = function (nums) {
// 创建循环长度,单调栈盒输出结果
const numsLength = nums.length;
const stack = [];
const result = new Array(numsLength).fill(0);
// 根据数组长度进行便利
for (let i = 0; i < numsLength; i++) {
// 通过大小进行判断,升序和降序
while (stack.length && nums[i] > nums[stack[stack.length - 1]]) {
// 符合要求移除栈顶
const pop = stack.pop();
// 移除的同时,进行相应的处理
result[pop] = i - pop;
}
stack.push(i);
}
return result;
};