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

题目大意
给定一个数组,在数组上每次步长为一滑动大小为k的窗口,然后求该窗口内按大小顺序排列好的一个中位数。

解法
这道题目没想出来,看的大神的解法。可以使用c++内的multiset,和一个指向最中间元素的iterator。multiset会维持内部的大小顺序,主要是依靠AVL实现的。首先,将数组内的前k个数放入multiset中,然后找到第k/2个迭代器,如果说k是奇数,那么该迭代器指向中位数,如果k是偶数,那么中位数就是第k/2个的值和第k/2-1的值的平均值,得到中位数后,添加到结果中。添加窗口后的数字,如果该数字小于中位数,那么中位数的指向要向前移动一位,然后再判断要踢出窗口的数字与中位数的大小,如果说小于等于的话,中位数要向后移动一位,等于的情况不能遗漏,如果遗漏了,那么此时中位数的迭代器指向的数字,已经被删除,还有在使用multiset.erase的时候,不要直接erase(num),这样只要multiset中某数的值为num,就会被删除,可能存在删除多个数的情况,而我们只需要删除一个元素。

class Solution {
public:
    vector<double> medianSlidingWindow(vector<int>& nums, int k) {
        vector<double> res;
        multiset<double> ms(nums.begin(), nums.begin() + k);
        auto mid = next(ms.begin(), k /  2);
        for (int i = k; ; ++i) {
            res.push_back((*mid + *prev(mid,  1 - k % 2)) / 2);
            if (i == nums.size()) return res;
            ms.insert(nums[i]);
            if (nums[i] < *mid) --mid;
            if (nums[i - k] <= *mid) ++mid;
            ms.erase(ms.lower_bound(nums[i - k]));
        }
    }
};

std::next
template< class ForwardIt >

ForwardIt next( ForwardIt it,

typename std::iterator_traits<ForwardIt>::difference_type n = 1 );

Return the nth successor of iterator it.

Parameters
it -- 迭代指针
n -- 向前进的元素个数,缺省默认为1

Return value
The nth successor of iterator it.(返回it的第n个后继迭代指针)

std::prev
使用方法与next相似,不同的是prev返回的是it的第n个前驱迭代指针

template< class BidirIt >

BidirIt prev( BidirIt it,

typename std::iterator_traits<BidirIt>::difference_type n = 1 );

原文地址:https://www.cnblogs.com/xiaobaituyun/p/8837609.html

时间: 2024-08-03 16:23:36

Leetcode480-Sliding Window Median的相关文章

[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

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的中间值,得到结果.每次进入新元

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.

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

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

Sliding Window Maximum 解答

Question 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. Example For array [1, 2, 7, 7, 8], movin

poj 2823 Sliding Window

poj 2823 Sliding Window 单调队列 单调队列是一个单调的状态(递增,或者递减) 所以需要维护这么一个状态 http://baike.baidu.com/link?url=ZcGM7Hzo8zVQUU6Oqqq18SlCMJ92ts3I1aXwQGDZw_NiDDlzIIV9GKlfs3X1fcHVppZHOU31geHZG4cOcRZOAK 固定 k 区间的单调 队列,求 最小值,如果 两个元素 A B ,如果 A 的 下标 比 B 小,但是 A 的 值 比 B 大,那么在

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

POJ2823 Sliding Window(单调队列)

单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. ----------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<deque> #define rep(i,r) for(int i=0;i<r;i++) #define clr(x,c) memset