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:

Input: 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

题意:

给定一个长度为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性质,最大值一定在最右)

代码:

 class Solution {
public int[] maxSlidingWindow(int[] a, int k) {
// corner case
if(k <= 0) return new int[]{};
//TreeMap要求其key必须可比较大小
TreeMap<Integer, Integer> map = new TreeMap<>((o1,o2) -> o1 - o2);
int[] result = new int[a.length - k + 1]; for(int i = 0; i < a.length; i++){
// 1. add to bst
if(map.containsKey(a[i])){
map.put(a[i], map.get(a[i]) + 1 );
}else{
map.put(a[i], 1);
}
// 2. remove from bst when window sliding
if( i >= k){
map.put(a[i - k] , map.get(a[i - k]) - 1 );
if(map.get(a[i - k]) == 0 ){
map.remove(a[i - k]);
}
}
// 3. get max
if( i + 1 >= k){
result[ i - (k - 1)] = map.lastKey();
}
}
return result;
}

[leetcode]239. Sliding Window Maximum滑动窗口最大值的更多相关文章

  1. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  2. 239 Sliding Window Maximum 滑动窗口最大值

    给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...

  3. [LeetCode] Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. [leetcode] #239 Sliding Window Maximum (Hard)

    原题链接 题意: 给定一个数组数字,有一个大小为k的滑动窗口,它从数组的最左边移动到最右边.你只能在窗口看到k个数字.每次滑动窗口向右移动一个位置. 记录每一次窗口内的最大值,返回记录的值. 思路: ...

  5. leetcode 239 Sliding Window Maximum

    这题是典型的堆排序算法,只是比一般的堆算法多了删除的操作,有两件事需要做: 1 用一个hash表存储从输入数组索引到堆数组(用于实现堆的那个数组)所以的映射,以便在需要删除一个元素的时候能迅速定位到堆 ...

  6. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  7. 【LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum   Given an array nums, there is a sliding window of size k which is moving fr ...

  8. 【刷题-LeetCode】239. Sliding Window Maximum

    Sliding Window Maximum Given an array nums, there is a sliding window of size k which is moving from ...

  9. [Leetcode]双项队列解决滑动窗口最大值难题

    这道题是从优先队列的难题里面找到的一个题目.可是解法并不是优先队列,而是双项队列deque 其实只要知道思路,这一道题直接写没有太大的问题.我们看看题 给定一个数组 nums,有一个大小为 k 的滑动 ...

随机推荐

  1. PHP PDO prepare()、execute()和bindParam()方法详解

    每次将查询发送给MySQL服务器时,都必须解析该查询的语法,确保结构正确并能够执行.这是这个过程中必要的步骤,但也确实带来了一些开销.做一次是必要的,但如果反复地执行相同的查询,批量插入多行并只改变列 ...

  2. javascript创建对象之原型模式(三)

    少废话,先上代码: function Human() { } Human.prototype.name = "成吉思汗"; Human.prototype.sex = " ...

  3. centos 7.x设置守护进程的文件数量限制

    在Bash中有个ulimit命令,提供了对Shell及该Shell启动的进程的可用资源控制.主要包括打开文件描述符数量.用户的最大进程数量.coredump文件的大小等. 1. 系统级设置 1.1 C ...

  4. 对象DIY

    1.在java开发中,好代码都是组织的比较好,模拟现实很好,而不是步骤指令 2.对象组织+继承(归类

  5. CSS3基础知识(一)

    结构选择器 :nth-child(n) 第几个元素 从1开始设置 :nth-child(2n) 偶数元素 从0开始设置 :nth-child(2n+1) 奇数元素 :nth-of-type(n) :f ...

  6. 搭建Discuz论坛

    准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Discuz 论坛系统依赖的基础运行环境.我们先来准备 LAMP 环境 安装 MySQL 使用 yum ...

  7. 内置锁(二)synchronized下的等待通知机制

    一.等待/通知机制的简介 线程之间的协作:   为了完成某个任务,线程之间需要进行协作,采取的方式:中断.互斥,以及互斥上面的线程的挂起.唤醒:如:生成者--消费者模式.或者某个动作完成,可以唤醒下一 ...

  8. restful 注解 总结 (比较完整的):http://www.xuetimes.com/archives/388 , https://www.cnblogs.com/chen-lhx/p/5599806.html

    参考1:  http://www.xuetimes.com/archives/388 参考2:   https://www.cnblogs.com/chen-lhx/p/5599806.html 参考 ...

  9. PHP 使用协同程序实现合作多任务

    多任务协作 如果阅读了上面的logger()例子,那么你认为“为了双向通信我为什么要使用协程呢? 为什么我不能只用常见的类呢?”,你这么问完全正确.上面的例子演示了基本用法,然而上下文中没有真正的展示 ...

  10. SQL2014还原到2008

    请使用with move选项来标识该文件的有效位置 sqlserver用命令还原数据库 restore   database   TT     from   disk='E:\test.bak'    ...