480 Sliding Window Median 滑动窗口中位数
详见:https://leetcode.com/problems/sliding-window-median/description/
C++:
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]));
}
}
};
参考:http://www.cnblogs.com/grandyang/p/6620334.html
480 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 Maximum 滑动窗口最大值
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- POJ - 2823 Sliding Window (滑动窗口入门)
An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...
- [leetcode]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 a ...
- Sliding Window(滑动窗口)
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 58002 Accepted: 16616 Case Time Limi ...
- [LeetCode] 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 a ...
- LeetCode 480. Sliding Window Median
原题链接在这里:https://leetcode.com/problems/sliding-window-median/?tab=Description 题目: Median is the middl ...
- POJ 2823 Sliding Window (滑动窗口的最值问题 )
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 41264 Accepted: 12229 ...
- 239 Sliding Window Maximum 滑动窗口最大值
给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口 k 内的数字.滑动窗口每次只向右移动一位.例如,给定 nums = [1,3,-1,-3, ...
随机推荐
- Spring中的AOP(学习笔记)
是什么AOP及实现方式 AOP的基本概念 Schema-base AOP Spring AOP API AspectJ
- Docker and Go: why did we decide to write Docker in Go?
Docker and Go: why did we decide to write Docker in Go? | Hacker News https://news.ycombinator.com/i ...
- 简说 call() 、apply() 、bind()
对于这三个方法,我想一部分人还是比较陌生的. 所以今天来个简单的介绍~ 我们可以将call()和apply()看作是某个对象的方法,通过调用方法的形式来间接调用函数.call()和apply()的第一 ...
- chrome——关于chrome浏览器的奇葩问题
前言 说下自己遇到的关于chrome的奇葩问题~ 问题 目前就一个,还是刚才才遇到的~ 消息通知 客户的chrome浏览器死活没有通知,检查后发现通知权限未开启, 通知权限开启后,还是没有提示,最后排 ...
- MYSQL进阶学习笔记二:MySQL存储过程和局部变量!(视频序号:进阶_4-6)
知识点三:MySQL存储过程和局部变量(4,5,6) 存储过程的创建: 创建存储过程的步骤: 首先选中数据库 改变分隔符,不让分号作为执行结束的标记.(通常情况下,改变分隔符命令 DELIMI ...
- [POI 2014] Couriers
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3524 [算法] 首先离线 , 将询问按右端点排序 如果我们知道[l , r]这个区间 ...
- bzoj4557 [JLoi2016]侦察守卫——DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4557 见这位的博客:https://www.cnblogs.com/Narh/p/91403 ...
- 【Cocos2d-HTML5 开发之一】新建HTML5项目及简单阐述与cocos2d/x引擎关系
真的是有一段时间没写博了,这段时间呢,发生的事情真的挺多,另外自己呢也闲来做了一些自己的喜欢的东西,主要做的还是基于Mac系统的Cocoa框架的各种编辑器吧.(对了,今年初也出了自己第二本书<i ...
- java try·····catch·····异常处理学习
异常处理(又称为错误处理)功能 用于处理软件或信息系统中出现的异常状况(即超出程序正常执行流程的某些特殊条件). try....catch....只是异常处理的一种常用方法 try{ //可能导致异常 ...
- zabbix 监控mysql状态 -Windows
由于公司爬虫mysql数据库在Windows下面,监控一下: 主要命令:mysqladmin -uroot -proot -h127.0.0.1 ping 2>D:\zabbix\waring. ...