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. javascript的面向对象思想知识要点

    获取数据类型 typeof undefined:访问某个不存在的或未经赋值的变量时就会得到一个 undefined,用typeof 获取类型,得到的也是undefined;null:它不能通过java ...

  2. 【Linux_Unix系统编程】chapter7 内存分配

    Chapter7 内存分配本章将用于在堆或者栈上分配内存的函数.7.1 在堆上分配内存 通常将堆的当前的内存边界称为"program break" 7.1.1 调整program ...

  3. 学习MongoDB 六: MongoDB查询(游标操作、游标信息)(三)

    一.简介 db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段.并返回到匹配文档的游标,可以随意修改查询限制.跳跃.和排序顺序的 ...

  4. js基础和运算符

    1.什么JavaScript?    运行环境 :    浏览器                    是一种具有安全性的客户端的脚本语言     用来实现与web页面交互 脚本语言:语言嵌入到htm ...

  5. ATM+购物商城

    知识内容: 1.luffy买tesla 2.ATM+购物商城 一.luffy买tesla 需求: 1.目录结构说明 account luffy.json --> 存储用户账户信息 {" ...

  6. Requests库入门

    安装: $ pip install requests Response对象的一些基本属性: Response.status_code 请求的返回状态,正常为200 Response.text 页面的字 ...

  7. jsp button onclick

    <input type="button" value="MD5哈希转换" onclick="javascript:document.getEle ...

  8. Unresolved external CheckAutoResult

    // [Linker   Error]Unresolved   external   'System::__linkproc__   __fastcall   CheckAutoResult() '  ...

  9. FireFox 火狐主页被劫持

    火狐主页被劫持hao123,流氓 WIN7 ,firefox,任务栏,快速启动,右键 属性 target 应该是 "D:\Program Files (x86)\Mozilla Firefo ...

  10. Spring事务控制和传递性理解

    1.在同一类方法间相互调用,如果调用方无事务控制,被调用方有事务控制,则被调用方也无事务 原因:外部经过spring容器调用service的方法事务才生效,service类内部方法间相互调用事务不生效 ...