Leetcode: Sliding Window Median

 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                Median
14 ---------------               -----
15 [1  3  -1] -3  5  3  6  7       1
16  1 [3  -1  -3] 5  3  6  7       -1
17  1  3 [-1  -3  5] 3  6  7       -1
18  1  3  -1 [-3  5  3] 6  7       3
19  1  3  -1  -3 [5  3  6] 7       5
20  1  3  -1  -3  5 [3  6  7]      6
21 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<Double> high = new PriorityQueue();
 3     PriorityQueue<Double> 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<nums.length; i++) {
11             if (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 }
时间: 2024-11-04 12:04:59

Leetcode: Sliding Window Median的相关文章

lintcode Sliding Window Median

http://www.lintcode.com/en/problem/sliding-window-median/ 最近感觉自己需要让心情平静下来,慢慢做事情,做好实验室的事情保证自己的毕业,同时也要做好题目找到份好工作. 先从写博客开始吧,把做过的题目记下来,写写思路. 这道题我没有用O(n log n)的方法,实际上应该是O(n log k),需要用两个set互相之间倒数据就可以了,判断起来比较烦. 直接用map也能够通过,线性查找sliding window的中间值,得到结果.每次进入新元

Sliding Window Median

Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration from the start of the array, find the maximum number inside the window at each moving. Have you met this question in a real interview? Y

[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 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. For example,Given nums

[Swift]LeetCode480. 滑动窗口中位数 | Sliding Window Median

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. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 Given an a

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 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. For example,Give

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 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. For example, Given

[Leetcode] Sliding Window Summary

Template from https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems For most substring problem, we are given a string and need to find a substring of it which satisfy

[LeetCode][JavaScript]Sliding Window Maximum

Sliding Window Maximum 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.

【Leetcode】Sliding Window Maximum

题目链接:https://leetcode.com/problems/sliding-window-maximum/ 题目: 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 sli