[leetcode]239. Sliding Window Maximum滑动窗口最大值
2021-04-04 01:29
标签:and array 取出 you for 数据 out span one Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Example: 题意: 给定一个长度为k的滑动窗口不断从左往右滑动,给出过程中的各个最大值。 思路: 使用一个每次能取出极值的数据结构,TreeMap,如下图,其底层用BST来储存 TreeMap要求key必须是比较大小(自然排序或定制排序) 以[1,1,-1,-3,5,3,6,7], k = 3 为例, 遍历数组,将数组每个元素作为TreeMap的key, 将该元素出现频率作为对应value [1, 1, -1, -3, 5, 3, 6, 7] ^ i = 0 [1, 1, -1, -3, 5, 3, 6, 7] ^ i = 1 [1, 1, -1, -3, 5, 3, 6, 7] ^ i = 2 [1, 1, -1, -3, 5, 3, 6, 7] ^ i = 3 此时 i >= k 则先将a[i-k]在TreeMap中对应的出现频率(value) 减1 再check一下 a[i-k]对应的value是否为0,为0则直接删去。 此例中,a[i-k] = 1, 在TreeMap中对应的value为2,那么value减1 后为1, 仍然继续保留。 由此可以看出,大体思路是用TreeMap维护一个所有value值相加为K的BST 用lastKey()来取出当前TreeMap里最大值(根据BST性质,最大值一定在最右) 代码: [leetcode]239. Sliding Window Maximum滑动窗口最大值 标签:and array 取出 you for 数据 out span one 原文地址:https://www.cnblogs.com/liuliu5151/p/9189712.htmlInput: nums = [1,3,-1,-3,5,3,6,7], and k = 3
Output: [3,3,5,5,6,7]
Explanation:
Window position Max
--------------- -----
[1 3 -1] -3 5 3 6 7 3
1 [3 -1 -3] 5 3 6 7 3
1 3 [-1 -3 5] 3 6 7 5
1 3 -1 [-3 5 3] 6 7 5
1 3 -1 -3 [5 3 6] 7 6
1 3 -1 -3 5 [3 6 7] 7
1 class Solution {
2 public int[] maxSlidingWindow(int[] a, int k) {
3 // corner case
4 if(k return new int[]{};
5 //TreeMap要求其key必须可比较大小
6 TreeMap
文章标题:[leetcode]239. Sliding Window Maximum滑动窗口最大值
文章链接:http://soscw.com/index.php/essay/71909.html