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. CORS跨域的概念与TP5的解决方案

    namespace app\api\behavior; use think\Response; class CORS{ public function appInit(&$params) { ...

  2. xshell配置通过堡垒机直接登陆到内网机器

    在xshell中文件-->新建菜单,打开新建会话属性,填写堡垒机的IP端口和账号密码后,进入登录脚本 : 勾选"执行以下的期望和发送组合对(X) " (1)添加: 期望: 发 ...

  3. win10的坑之wifi找不到

    安装了win10一周以来,win10的坑太多太多,微软搞什么pc/mobile二合一,真是脑残行为. 首先是usb设备无缘无故找不到,据说是和杀毒软件/防火墙有关,后来是关掉了windows defe ...

  4. javascript继承之借用构造函数(二)

    //简单的函数调用 function Father() { this.nums= [1,2]; } function Son() { Father.call(this);//调用超类型,完成son继承 ...

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

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

  6. plsql 执行批量文件

    plsql 执行批量文件 plsql>command window @c:\a.sql;@c:\b.sql;@c:\c.sql;

  7. Spring cron 定时调度配置

    IDEA 或者 STS http://spring.io/guides/gs/scheduling-tasks/ spring  mvc : 结构: Seconds Minutes Hours Day ...

  8. java自定义线程池

    如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁创建线程和销毁线程需要时间.那么有没有一种办法使得线程可以复用,就是执行完一个任 ...

  9. ABAP-ITS Mobile

    ITS Mobile是14年开发EWM项目时用到的技术方案,本文主要记录下ITS Mobile的整个实现过程. 1.ITS Mobile介绍 ITS Mobile:Internet Transacti ...

  10. windows10配置java开发环境

    一.下载jdk 二.安装jdk路径,这个路径不能包含中文名 三.系统会提示安装jre,安装目录不要是jdk的安装目录,否则会覆盖掉jdk目录下的jre目录 四. .;%JAVA_HOME%\lib;% ...