博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Sliding Window Median
阅读量:5871 次
发布时间:2019-06-19

本文共 2857 字,大约阅读时间需要 9 分钟。

1 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. 2  3 Examples:  4 [2,3,4] , the median is 3 5  6 [2,3], the median is (2 + 3) / 2 = 2.5 7  8 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. Your job is to output the median array for each window in the original array. 9 10 For example,11 Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.12 13 Window position                Median14 ---------------               -----15 [1  3  -1] -3  5  3  6  7       116  1 [3  -1  -3] 5  3  6  7       -117  1  3 [-1  -3  5] 3  6  7       -118  1  3  -1 [-3  5  3] 6  7       319  1  3  -1  -3 [5  3  6] 7       520  1  3  -1  -3  5 [3  6  7]      621 Therefore, return the median sliding window as [1,-1,-1,3,5,6].

方法1:Time Complexity O(NK)

暂时只有两个Heap的做法,缺点:In this problem, it is necessary to be able remove elements that are not necessarily at the top of the heap. PriorityQueue has logarithmic time remove top, but a linear time remove arbitrary element.

For a Heap:

remove():  Time Complexity is O(logN)

remove(Object): Time Complexity is O(N)

更好的有multiset的方法,但是还没有看到好的java version的

最大堆的简单定义方法:Collections.reverseOrder(), Returns a comparator that imposes the reverse of the natural ordering on a collection of objects 

1 public class Solution { 2     PriorityQueue
high = new PriorityQueue(); 3 PriorityQueue
low = new PriorityQueue(Collections.reverseOrder()); 4 5 6 public double[] medianSlidingWindow(int[] nums, int k) { 7 double[] res = new double[nums.length-k+1]; 8 int index = 0; 9 10 for (int i=0; i
= k) remove(nums[i-k]);12 add((double)nums[i]);13 if (i >= k-1) {14 res[index++] = findMedian();15 }16 }17 return res;18 }19 20 public void add(double num) {21 low.offer(num);22 high.offer(low.poll());23 if (low.size() < high.size()) {24 low.offer(high.poll());25 }26 }27 28 public double findMedian() {29 if (low.size() == high.size()) {30 return (low.peek() + high.peek()) / 2.0;31 }32 else return low.peek();33 }34 35 public void remove(double num) {36 if (num <= findMedian()) {37 low.remove(num);38 }39 else {40 high.remove(num);41 }42 if (low.size() < high.size()) {43 low.offer(high.poll());44 }45 else if (low.size() > high.size()+1) {46 high.offer(low.poll());47 }48 }49 }

 

转载地址:http://bbhnx.baihongyu.com/

你可能感兴趣的文章
icmp
查看>>
java的接口和抽象类区别
查看>>
表生成器@TableGenerator
查看>>
我眼中的PM
查看>>
sqrt()平方根计算函数的实现1——二分法
查看>>
Web前端研发工程师编程能力飞升之路
查看>>
Linux内核 设备树操作常用API【转】
查看>>
“安装程序无法定位现有系统分区,也无法创建新的系统分区”提示
查看>>
volatile的深入理解--【sky原创】
查看>>
RabbitMQ指南之二:工作队列(Work Queues)
查看>>
js提交图片转换为base64
查看>>
面向对象 委托
查看>>
PassWord控件
查看>>
【带着canvas去流浪(5)】绘制K线图
查看>>
Linux 删除mysql数据库失败的解决方法
查看>>
浏览器缓存文件导致js文件更改无效
查看>>
如何才能学好javascript
查看>>
学习CodeIgniter框架之旅(二)继承自定义类
查看>>
yum被锁Another app is currently holding the yum lock; waiting for it to exit...
查看>>
Excel .net读取
查看>>