LeetCode 480. Sliding Window Median

原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description

题目:

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 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.

For example,
Given nums = [1,3,-1,-3,5,3,6,7], and k = 3.

Window position                Median
---------------               -----
[1  3  -1] -3  5  3  6  7       1
 1 [3  -1  -3] 5  3  6  7       -1
 1  3 [-1  -3  5] 3  6  7       -1
 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]      6

Therefore, return the median sliding window as [1,-1,-1,3,5,6].

Note: 
You may assume k is always valid, ie: 1 ≤ k ≤ input array's size for non-empty array.

题解:

Use two treeset to maintain the median. one treeset keeps the lower half, one treeset keeps the higher half.

Here we keep the index, but not the actual value. Why not the actual value, because there could be duplicate, and set will keep duplicates.

Keeps making sure the higer treeset size is not smaller.

When both treeset size sum to k, then starts to update the median to the res.

If both size are equal, peek the last of lower treeset and first of higher treeset and get the average.

If not equal, get the first of higher treeset as median.

Remove i - k + 1 from both treesets.

Note: 两遍peek求和时注意overflow.

When using lambda, use Integer.compare(nums[a], numb[b]), but not nums[a] - nums[b]. This is because nums[a] - nums[b] could be overflow in Java.

Time Complexity: O(nlogk), n = nums.length. add and remove to treeset takes O(logk).

Space: O(k).

AC java:

 1 class Solution {
 2     public double[] medianSlidingWindow(int[] nums, int k) {
 3         if(nums == null || nums.length == 0 || k <= 0){
 4             return new double[0];
 5         }
 6 
 7         int n = nums.length;
 8         double[] res = new double[n - k + 1];
 9         TreeSet<Integer> lo = new TreeSet<Integer>((a, b) -> nums[a] == nums[b] ? Integer.compare(a, b) : Integer.compare(nums[a], nums[b]));
10         TreeSet<Integer> hi = new TreeSet<Integer>((a, b) -> nums[a] == nums[b] ? Integer.compare(a, b) : Integer.compare(nums[a], nums[b]));
11         for(int i = 0; i < n; i++){
12             hi.add(i);
13             lo.add(hi.pollFirst());
14             if(lo.size() > hi.size()){
15                 hi.add(lo.pollLast());
16             }
17 
18             if(lo.size() + hi.size() == k){
19                 res[i - k + 1] = lo.size() == hi.size() ? ((double)nums[lo.last()] + (double)nums[hi.first()]) / 2.0 : nums[hi.first()];
20                 lo.remove(i - k + 1);
21                 hi.remove(i - k + 1);
22             }
23         }
24 
25         return res;
26     }
27 }

类似Find Median from Data Stream

posted @ 2017-02-08 16:00  Dylan_Java_NYC  阅读(650)  评论(0编辑  收藏  举报