《Cracking the Coding Interview》——第18章:难题——题目9
2014-04-29 04:18
题目:有一连串的数被读入,设计一个数据结构,能随时返回当前所有数的中位数。
解法:用一个大顶堆,一个小顶堆将数分成数量最接近的两份,就能轻松得到中位数了。
代码:
// 18.9 A stream of integers are passed to you, you have to tell me the median as they keep coming in.
#include <climits>
#include <iostream>
#include <vector>
#include <queue>
using namespace std; template <class T>
struct LessFunctor
{
bool operator() (const T &x, const T &y)
{
return x < y;
}
}; template <class T>
struct GreaterFunctor
{
bool operator() (const T &x, const T &y)
{
return x > y;
}
}; template <class T>
class MedianArray {
public:
MedianArray() {
n_small = ;
n_great = ;
}; void push(const T& val) {
if (n_great == ) {
greater_heap.push(val);
++n_great;
return;
} if (n_great > n_small) {
smaller_heap.push(val);
++n_small;
} else {
greater_heap.push(val);
++n_great;
} if (greater_heap.top() < smaller_heap.top()) {
T tmp; tmp = greater_heap.top();
greater_heap.pop();
greater_heap.push(smaller_heap.top());
smaller_heap.pop();
smaller_heap.push(tmp);
}
}; T median() {
if (n_great == ) {
return INT_MIN;
} else if (n_great > n_small) {
return greater_heap.top();
} else {
return (smaller_heap.top() + greater_heap.top()) / ;
}
}; ~MedianArray() {
n_small = ;
n_great = ;
while (!greater_heap.empty()) {
greater_heap.pop();
}
while (!smaller_heap.empty()) {
smaller_heap.pop();
}
};
private:
int n_small; // greater elements are stored in here.
priority_queue<T, vector<T>, GreaterFunctor<T> > greater_heap; int n_great; // smaller elements are stored in here.
priority_queue<T, vector<T>, LessFunctor<T> > smaller_heap;
}; int main()
{
MedianArray<int> ma;
int val; while (cin >> val) {
ma.push(val);
cout << ma.median() << endl;
} return ;
}
《Cracking the Coding Interview》——第18章:难题——题目9的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目12
2014-04-29 04:36 题目:最大子数组和的二位扩展:最大子矩阵和. 解法:一个维度上进行枚举,复杂度O(n^2):另一个维度执行最大子数组和算法,复杂度O(n).总体时间复杂度为O(n^3 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- 浏览器中使用calc不识别
在使用css3中的calc运算函数时,发现浏览器不识别,当时代码是这样的 width:calc(100%-50px); 经过查询官网原来发现这里有个需要注意的地方就是在进行加减运算的时候,必须在运算符 ...
- 如何使用事务码SMICM分析ABAP代码发起的HTTP请求的错误ICM_HTTP_SSL_PEER_CERT_UNTRUSTED
当我用CL_HTTP_CLIENT往一个外网的url发请求时,遇到错误:ICM_HTTP_SSL_PEER_CERT_UNTRUSTED 错误是从这段ABAP代码里抛出来的: CALL METHOD ...
- 如何计算并测量ABAP及Java代码的环复杂度Cyclomatic complexity
代码的环复杂度(Cyclomatic complexity,有的地方又翻译成圈复杂度)是一种代码复杂度的衡量标准,在1976年由Thomas J. McCabe, Sr. 提出. 在软件测试的概念里, ...
- IOS 公司标示和方向域名
1. 公司标示使用反向域名========================================正向域名 www.baidu.com 用来标示一台网络主机反向域名 cn.itcast.Myd ...
- jQuery 效率提升建议
jQuery简洁通用的方法集把编码者从繁重的工作中解脱出来,也拉低了进入javascript的门槛,初学者对浏览器兼容性一无所知的情况下,几行代码就可以写出超炫的特效.网上有一篇文章转载比较泛滥,已经 ...
- 1.1 NBU基本概念
1.1 NBU基本概念 1) Policy(备份策略) 备份策略定义一台或几台服务器的备份方法.它包括哪些服务器需要备份.备份哪些目录或文件.在什么时间备份.采用什么方式进行备份等.配置NBU主要指定 ...
- 2017年10月26日 git上传文件失败的文件
最近几天因为项目要用git,于是学习了一下git.今天上传项目到码云的时候,却发现总有一些文件夹上传不上去,git 也显示everything is update.找了一圈办法,都没有用,最后突然发现 ...
- Exception occurred during processing request: The given object has a null identifier: com.zsn.crm.Model.SaleVisit; nested exception is org.hibernate.TransientObjectException: The given object has a nu
edit.jsp页面没有加入隐藏字段 id ,导致模型驱动封装时缺少id ,,调用update更新数据库时出错!
- #Python编程从入门到实践#第三章笔记
列表简介 1.什么是列表 列表:由一系列按也顶顺序排列的元素组成.元素之间可以没有任何关系. 列表:用方括号[]表示,并用逗号分隔其中元素.名称一般为复数 2.访问元素 (1)列表是有序集合 ...
- 18式优雅你的Python
本文来自读者梁云同学的投稿,公众号:Python与算法之美 一,优雅你的Jupyter 1,更改Jupyter Notebook初始工作路径 平凡方法: 在cmd中输入jupyter notebook ...