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 / );
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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- LeetCode 480. Sliding Window Median
原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
- LintCode "Sliding Window Median" & "Data Stream Median"
Besides heap, multiset<int> can also be used: class Solution { void removeOnly1(multiset<in ...
- Lintcode360 Sliding Window Median solution 题解
[题目描述] Given an array of n integer, and a moving window(size k), move the window at each iteration f ...
- 滑动窗口的中位数 · Sliding Window Median
[抄题]: 给定一个包含 n 个整数的数组,和一个大小为 k 的滑动窗口,从左到右在数组中滑动这个窗口,找到数组中每个窗口内的中位数.(如果数组个数是偶数,则在该窗口排序数字后,返回第 N/2 个数字 ...
- Sliding Window Median
Description Given an array of n integer, and a moving window(size k), move the window at each iterat ...
- 480 Sliding Window Median 滑动窗口中位数
详见:https://leetcode.com/problems/sliding-window-median/description/ C++: class Solution { public: ve ...
随机推荐
- (转)SpringBoot之退出服务(exit)时调用自定义的销毁方法
我们在工作中有时候可能会遇到这样场景,需要在退出容器的时候执行某些操作.SpringBoot中有两种方法可以供我们来选择(其实就是spring中我们常用的方式.只是destory-method是在XM ...
- Hybrid设计--核心交互
普通网页中跳转使用a标签,这里我们要对跳转进行更多的干预,所以将全站的跳转收口到框架层,用forward去实现.拒绝用a和window.location.如果我想对所有跳转做一个处理,开动画或者对跳转 ...
- come on! linux install JDK9.0.1
哈哈,JDK9已经出来了 我们把它安装到Linux上吧! 一下载JDK9 http://www.oracle.com/technetwork/java/javase/downloads/index.h ...
- 14.ajax基础知识、用ajax做登录页面、用ajax验证用户名是否可用、ajax动态调用数据库
1.ajax的基础知识 ajax是结合了jquery.php等几种技术延伸出来的综合运用的技术,不是新的内容.ajax也是写在<script>标签里面的. 如果使用ajax一定是要有1个处 ...
- InstallShield2015制作安装包----------安装过程中修改文件内容
//修改安装目录下autostart.vbs里的路径 //打开文件 OpenFileMode(FILE_MODE_NORMAL); strPath=INSTALLDIR+"centerAut ...
- php 文件远程下载
getFile(“http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg”,”,’xuxin’);/*** php实现下载远程图片保存到本地 ...
- Nginx使用rewrite重新定向
[Rewrite重定向]Nginx使用rewrite重新定向 使用nginx做重新定向. nginx参考网址:http://blog.sina.com.cn/s/blog_97688f8e0100 ...
- hibernate添加数据入门小案例
1.建立一个java项目,在目录下新建一个lib文件夹引入hibernate架包如图所示: 2. 新建com.LHB.domain包,在包中分别创建一个Employee.java和Employee.h ...
- Android -- 仿小米锁屏画报
1,首先看一下我们今天实现的效果,效果图如下: 2,首先说一下大体的实现思路,首先这个视图一共分为三层,最外层是一个RecyclerView,第二层是一个被虚化的ImageView,第三层是一个正常的 ...
- tcl脚本
tcl,全名tool command language,是一种通用的工具语言. 1)每个命令之间,通过换行符或者分号隔开: 2)tcl的每个命令包含一个或者多个单词,默认第一个单词表示命令,第二个单词 ...