18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated.

LeetCode上的原题,请参见我之前的博客Find Median from Data Stream.

解法一:

priority_queue<int> small;
priority_queue<int, vector<int>, greater<int>> large; void addNum(int num) {
small.push(num);
large.push(small.top());
small.pop();
if (small.size() < large.size()) {
small.push(large.top());
large.pop();
}
} double find_median() {
return small.size() > large.size() ? small.top() : 0.5 * (small.top() + large.top());
}

解法二:

priority_queue<int> small, large;

void addNum(int num) {
small.push(num);
large.push(-small.top());
small.pop();
if (small.size() < large.size()) {
small.push(-large.top());
large.pop();
}
} double find_median() {
return small.size() > large.size() ? small.top() : 0.5 * (small.top() - large.top());
}

CareerCup All in One 题目汇总

[CareerCup] 18.9 Find and Maintain the Median Value 寻找和维护中位数的更多相关文章

  1. [CareerCup] 18.1 Add Two Numbers 两数相加

    18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...

  2. [CareerCup] 18.12 Largest Sum Submatrix 和最大的子矩阵

    18.12 Given an NxN matrix of positive and negative integers, write code to find the submatrix with t ...

  3. [CareerCup] 18.11 Maximum Subsquare 最大子方形

    18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an ...

  4. [CareerCup] 18.10 Word Transform 单词转换

    18.10 Given two words of equal length that are in a dictionary, write a method to transform one word ...

  5. [CareerCup] 18.8 Search String 搜索字符串

    18.8 Given a string s and an array of smaller strings T, design a method to search s for each small ...

  6. [CareerCup] 18.6 Smallest One Million Numbers 最小的一百万个数字

    18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume t ...

  7. [CareerCup] 18.5 Shortest Distance between Two Words 两单词间的最短距离

    18.5 You have a large text file containing words. Given any two words, find the shortest distance (i ...

  8. [CareerCup] 18.4 Count Number of Two 统计数字2的个数

    18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如 ...

  9. [CareerCup] 18.3 Randomly Generate Integers 随机生成数字

    18.3 Write a method to randomly generate a set of m integers from an array of size n. Each element m ...

随机推荐

  1. win7-32 系统 + VS2010 配置 glew

    网上下载的程序,运行时报错:  C1083: 无法打开包括文件:“gl\glew.h”: No such file or directory. 百度一下,发现需要配置 glew 库. 方法如下: 下载 ...

  2. JS 获取元素当前的样式信息

    HTMLElement.prototype.__defineGetter__("currentStyle", function () { return this.ownerDocu ...

  3. 智能车学习(二十一)——浅谈CCD交叉以及横线摆放

    一.CCD为何要交叉摆放?       首先使用横线摆放,CCD前瞻如果远一点,弯道丢线,再远一点直接窜道.所以需要很多很多代码的工作量,而且过弯的过程相当于没有任何的调节过程,就是一个偏差保持,或者 ...

  4. AIDL

    在介绍跨程序进程间通信AIDL前,先看一下本程序activity与某个服务是怎么绑定在一起进行交互的. 需求:服务有两个方法.分别是播放音乐与停止播放音乐.该程序的活动要访问这两个方法,在activi ...

  5. C#分布式缓存Couchbase使用

    目前C#业界使用得最多的 Cache 系统主要是 Memcached和 Redis. 这两个 Cache 系统可以说是比较成熟的解决方案,也是很多系统当然的选择. 一.简介 目前C#业界使用得最多的 ...

  6. 查询sqlserver 正在执行的sql语句的详细信息

    SELECT [Spid] = session_Id, ecid, [Database] = DB_NAME(sp.dbid), [User] = nt_username, [Status] = er ...

  7. SQLServer 维护脚本分享(04)服务器角色和数据库角色相关操作

    /*------------------------------------------------------------------------------------ [服务器级别-服务器角色] ...

  8. 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

    当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...

  9. JSON.parse()和JSON.stringify()使用

    JSON.parse()用于从一个字符串中解析出json对象,如 var str = '{"name":"huangxiaojian","age&qu ...

  10. hashlib加密操作模块

    import hashlib#加密操作obj=hashlib.md5(bytes("hasdfghjklcxz",encoding="utf-8"))#加密操作 ...