《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’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
随机推荐
- MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权
在项目下新建一个文件夹来专门放过滤器类,首先创建一个类LoginFilter,这个类继承ActionFilterAttribute.用来检查用户是否登录和用户权限.: using System; us ...
- kk录像机怎么剪辑视频 kk录像机视频剪辑教程
很多朋友录制视频都是用KK录像机,录制视频过后我们需要对视频进行修改和调整,下面小编就教大家怎么来剪辑KK录像机录制的视频 1.首先我们打开软件点[添加一个视频],添加需要剪切的视频 2.将播放指针移 ...
- 有趣的回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- Selenium入门20 等待时间
自动化过程中有的页面元素加载慢或者需要等待特定条件执行后续步骤,此时需添加等待时间: 1 time.sleep() 固定等待时间,需import time 2 webdriver隐式等待 无需引入包 ...
- CRUD全栈式编程架构之MVC的扩展设计
MVC执行流程 路由的扩展 我理解的路由作用有以下几个 Seo优化,用“/”分开的url爬虫更爱吃 物理和逻辑文件分离,url不再按照文件路径映射 Controller,Action的选择 MVC路由 ...
- 1.5配置NetBackup数据库备份策略(nbu策略catalog)
1.5配置NetBackup数据库备份策略 建议定期备份NetBackup的索引数据库Catalog,以确保故障时的有效恢复.从Javaconsole可以进入备份NetBackup内部数据库配置窗口, ...
- 虚方法(virsual method)
虚方法(virsual method)挺起来玄乎其玄,向从未听说过这个概念的人解释清楚是一件相当困难的事情. 因为这是一个很不容易理解的概念,但它在比较抽象的代码里边是不可少的. 那么既然用枯燥的文字 ...
- 遗忘的html标签
<span>x</span><sup>2</sup><span>+y=10</span> <br> <span ...
- xcode怎样分析检测内存泄露(iOS)
虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是可能存在.所以了解原理很重要. 这里讲述在没有ARC的情况下,如何使用Instruments来查找程序中的内存泄露, ...
- BZOJ3884: 上帝与集合的正确用法(欧拉函数 扩展欧拉定理)
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 3860 Solved: 1751[Submit][Status][Discuss] Descripti ...