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. DWZ分页、排序失效小结

    1. 在视图文件中与分页相关的代码段 <form id="pagerForm" method="post" action="w_list.htm ...

  2. Oracle中“行转列”的实现方式

    在报表的开发当中,难免会遇到行转列的问题. 以Oracle中scott的emp为例,统计各职位的人员在各部门的人数分布情况,就可以用“行转列”: scott的emp的原始数据为: EMPNO ENAM ...

  3. Vue入门笔记#过渡

    Vue过渡,可以在元素从DOM中移除,插入时自动调用过渡效果.根据设定,会适时的触发过渡效果. 在使用的目标标签里添加 transition: <div transition="my_ ...

  4. Struts2请求参数校验

    校验的分类 客户端数据校验 和 服务器端数据校验 客户端数据校验 ,通过JavaScript 完成校验 (改善用户体验,使用户减少出错 ) 服务器数据校验 ,通过Java代码 完成校验 struts2 ...

  5. Loadrunner中百分比模式和Vuser模式

    从百分比模式切换到Vuser模式后,多个脚本时候,每个脚本的比例仍然维持不变: 切换到Vuser模式后: 如果在场景执行过程中需要动态添加Vuser,只能在Vuser模式下执行场景 如果需要执行“组” ...

  6. Windows Server 2008 R2 实现多用户同时登陆

    Windows Server 2008 R2远程用户数设置 在windows server 2008 R2里面,默认的远程桌面连接数为1.这对我们的服务器管理带来了很大的不便,那么怎样来修改2008 ...

  7. (转)set集合的用法

    原地址:http://blog.csdn.net/chaiwenjun000/article/details/50561775 SET 集合 百度百科中说集合中的元素有三个特征: 1.确定性(集合中的 ...

  8. wpf 触发器,属性触发器,事件触发器,事件触发器。

    <EventTrigger RoutedEvent="Mouse.MouseEnter"/> <DataTrigger Binding="{Bindin ...

  9. S5中新增的Array方法详细说明

      ES5中新增的Array方法详细说明 by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wor ...

  10. [转载] Spring MVC - 处理器拦截器

    5.1.处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器)类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.   ...