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 ...
随机推荐
- 实习培训——Java多线程(9)
实习培训——Java多线程(9) 很适合新手 http://www.cnblogs.com/GarfieldEr007/p/5746362.html http://www.cnblogs.com/Ga ...
- mac xcode 常见配置
1.报错:There are no schemes in workspace "..." 设置scheme共享,方法: 2.Build 文件夹是中间文件的保存地方,如何设置在工程目 ...
- node服务开发环境判断和启动端口指定---process.env.NODE_ENV
在node启动的时候我们需要在代码里面判断服务器运行环境 可以根据process.env.NODE_ENV来判断 一.开发环境的判断 1.安装 npm i -g cross-env 2.启动 cros ...
- Ubuntu install mysql database
简要说下ubuntu Linux下安装MySql数据库 一. 安装 # apt-get install mysql-server# apt-get install mysql-client 二.启动 ...
- 28.json数组,select选择,input输出对应数据
效果:选择对应的车类,下面的input会实时出现车价. html: <form action="" class="priceModelform1"> ...
- equals和==的区别小结
==: == 比较的是变量(栈)内存中存放的对象的(堆)内存地址,用来判断两个对象的地址是否相同,即是否是指相同一个对象.比较的是真正意义上的指针操作. 1.比较的是操作符两端的操作数是否是同一个对象 ...
- mysql 5.6 每天凌晨12:00 重置sequence表中的某个值
#.创建evevt要调用的存储过程update_current_value_procedure delimiter // drop procedure if exists update_current ...
- centos安装java的jdk
1.下载 jdk-8u101-linux-x64.rpm http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads- ...
- 5.JVM的内存区域划分
一.JVM介绍 1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟 ...
- B树,B+树,B*树以及R树的介绍
https://blog.csdn.net/peterchan88/article/details/52248714 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开 ...