题解 | #滑动窗口的最大值#单调栈
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
单调栈,递增栈
package main /** * * @param num int整型一维数组 * @param size int整型 * @return int整型一维数组 */ func maxInWindows(num []int, size int) []int { // write code here res := make([]int,0) stack := []int{} if len(num)<size||size==0{ return res } for i := 0; i < size; i++ { for len(stack) > 0 && stack[len(stack)-1] < num[i] { stack = stack[:len(stack)-1] } stack = append(stack, num[i]) } res = append(res, stack[0]) for i := size; i < len(num); i++ { if len(stack) > 0 && stack[0] == num[i-size] { stack = stack[1:] } for len(stack) > 0 && stack[len(stack)-1] < num[i] { stack = stack[:len(stack)-1] } stack = append(stack, num[i]) res = append(res, stack[0]) } return res }