题解 | #滑动窗口的最大值#
滑动窗口的最大值
https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788
import java.util.*; public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size) { ArrayList<Integer> res = new ArrayList<>(); if (num == null || size < 1 || num.length < size) { return res; } Deque<Integer> dq = new LinkedList<>(); for (int i = 0; i < num.length; i++) { while (!dq.isEmpty() && num[dq.peekLast()] < num[i]) { dq.pollLast(); } dq.offerLast(i); if (dq.peekFirst() + size <= i) { dq.pollFirst(); } if (i + 1 >= size) { res.add(num[dq.peekFirst()]); } } return res; } }