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 / );
for (int i = k; ; ++i) {
res.push_back((*mid + *prev(mid, - k % )) / );
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 );

Leetcode480-Sliding Window Median的更多相关文章

  1. [LeetCode] Sliding Window Median 滑动窗口中位数

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  2. Leetcode: Sliding Window Median

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  3. Sliding Window Median LT480

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. LeetCode 480. Sliding Window Median

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

  5. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  6. LintCode "Sliding Window Median" & "Data Stream Median"

    Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...

  7. Lintcode360 Sliding Window Median solution 题解

    [题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...

  8. 滑动窗口的中位数 · Sliding Window Median

    [抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...

  9. Sliding Window Median

    Description Given an array of n integer, and a moving window(size k), move the window at each iterat ...

  10. 480 Sliding Window Median 滑动窗口中位数

    详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...

随机推荐

  1. 用Partimage创建或恢复分区备份

    1 Preliminary Note Partimage is part of the system rescue CD found on http://www.sysresccd.org which ...

  2. 海量数据找相同数,高配词,不重复的数,判断一个数是否存在,查询串,不同电话号码的个数,中位数,按照query频度排序,topk

    这类题目,首先需要确定可用内存的大小,然后确定数据的大小,由这两个参数就可以确定hash函数应该怎么设置才能保证每个文件的大小都不超过内存的大小,从而可以保证每个小的文件都能被一次性加载到内存中. 1 ...

  3. php curl POST multipart/form-data与application/x-www-form-urlencode的区别

    背景 CURL在 a.php 中以 POST方式向 b.php 提交数据,但b.php无法接收到数据,而 CURL 操作显示成功. 原来,"传递一个数组到CURLOPT_POSTFIELDS ...

  4. PyQT5速成教程-4 Qt Designer实战[上]

    本文由 沈庆阳 所有,转载请与作者取得联系! 前言 在前面几节的学习中,我们对PyQt的基本使用.Qt Designer与Python编码的工作流程有了基本的学习.同时也掌握了Qt Designer中 ...

  5. html5闰年判断函数

    <script>    var year=2016;        function runnian(year){            if (year%400==0) {return ...

  6. 利用JavaCSV API来读写csv文件

    http://blog.csdn.net/loongshawn/article/details/53423121 http://javacsv.sourceforge.net/ 转载请注明来源-作者@ ...

  7. 更换tomcat运行日志目录

    1.在tomcat安装文件夹的bin目录下,修改catalina.sh,改变catalina.out的输出目录 CATALINA_OUT="$CATALINA_BASE"/logs ...

  8. [mysql]Date和String相互转换(DATE_FORMAT&STR_TO_DATE)

    1.Date  ——>  String 使用的函数:DATE_FORMAT(date,format)     date:需要转换的日期       format:格式化的样式 format样式整 ...

  9. ArrayList 底层实现原理

    ArrayList的底层实现原理 1, 属性:private static final int DEFAULT_CAPACITY = 10; private static final Object [ ...

  10. Class__Two

    今天老师要求做查找英文文章中最高频的词  文章用文本储存 import java.io.BufferedReader;import java.io.File;import java.io.FileIn ...