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