资讯专栏INFORMATION COLUMN

[LeetCode] 480. Sliding Window Median

freecode / 2716人阅读

摘要:存大于的数存小于的数保证总比的相等或多一个元素

Problem

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: k is always smaller than input array"s size for non-empty array.

Solution
class Solution {
    //minHeap存大于median的数
    //maxHeap存小于median的数
    PriorityQueue minHeap = new PriorityQueue<>();
    PriorityQueue maxHeap = new PriorityQueue<>((a, b)->(b.compareTo(a)));
    public double[] medianSlidingWindow(int[] nums, int k) {
        int n = nums.length-k+1;
        if (n <= 0) return new double[0];
        
        double[] res = new double[n];
        
        for (int i = 0; i <= nums.length; i++) {
            if (i >= k) {
                res[i-k] = getMedian();
                remove(nums[i-k]);
            }
            if (i < nums.length) {
                add(nums[i]);
            }
        }
        
        return res;
    }
    private void add(int num) {
        if (num < getMedian()) maxHeap.offer(num);
        else minHeap.offer(num);
        //保证minHeap总比maxHeap的size相等或多一个元素
        if (maxHeap.size() > minHeap.size()) minHeap.offer(maxHeap.poll());
        if (maxHeap.size() < minHeap.size()-1) maxHeap.offer(minHeap.poll());
    }
    private void remove(int num) {
        if (num < getMedian()) maxHeap.remove(num);
        else minHeap.remove(num);
        if (maxHeap.size() > minHeap.size()) minHeap.offer(maxHeap.poll());
        if (maxHeap.size() < minHeap.size()-1) maxHeap.offer(minHeap.poll());
    }
    private double getMedian() {
        if (maxHeap.isEmpty() && minHeap.isEmpty()) return 0;
        if (maxHeap.size() == minHeap.size()) return ((double) maxHeap.peek() + (double) minHeap.peek()) / 2.0;
        else return (double) minHeap.peek();
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72738.html

相关文章

  • 480. Sliding Window Median

    摘要:题目链接这题和那道比起来多加了个。还是用两个来做,这个操作复杂度用了。和,在保存较小的一半元素,保存较大的一半元素,,注意写的时候不能用,因为可能。没想出来其他方法,参考的解法 480. Sliding Window Median 题目链接:https://leetcode.com/problems... 这题和那道Find Median from Data Stream比起来多加了个...

    Scorpion 评论0 收藏0
  • [LintCode/LeetCode] Sliding Window Maximum/Median

    摘要:窗口前进,删队首元素保证队列降序加入当前元素下标从开始,每一次循环都将队首元素加入结果数组 Sliding Window Maximum Problem Given an array of n integer with duplicate number, and a moving window(size k), move the window at each iteration fro...

    crelaber 评论0 收藏0
  • [LeetCode] 239. Sliding Window Maximum

    摘要:丢弃队首那些超出窗口长度的元素队首的元素都是比后来加入元素大的元素,所以存储的对应的元素是从小到大 Problem 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...

    lentoo 评论0 收藏0
  • leetcode239. Sliding Window Maximum

    摘要:题目要求假设有一个数组和一个长度为的窗口,数组长度。当窗口右滑时,会删除下标上的值,并加入下标上的值。此时中记录的值编程了,并返回当前的最大值为。一旦最大值失效,就从窗口中重新找一个最大值就好了。 题目要求 Given an array nums, there is a sliding window of size k which is moving from the very lef...

    sPeng 评论0 收藏0
  • [Leetcode] 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...

    lvzishen 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<