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. vim用法小节

    1.把一个文件的内容全选复制到另一个文件 方案一: gg"*yG 然后另外一个vim "*p "*是系统剪贴板寄存器 方案二: 打开另一个文件,然后输入 :r filen ...

  2. Eclipse·如何关联Git库文件和添加JUint库

    Eclipse创建工程并关联到文件(SVN或Git管理的代码文件) 新建java工程,用于存放工程的一些信息,默认存放地址. 工程相关的信息是不需要提交到(SVN或Git)版本库的,所以工程存放到本地 ...

  3. HorizontalScrollView

    HorizontalScrollView 链接

  4. JDBC之——一个单线程JDBC基类和一些注意事项

    下面的一个DBase基类提供了一些jdbc的基本操作,配置文件的配置部分并不准确,只是在自己机器上的mysql中测试过.. 数据库的增删改方法中都在开始调用了getConnection方法,末尾调用了 ...

  5. Hibernate 延迟加载

    一.什么是延迟加载? 延迟加载是指当应用程序想要从数据库获取对象时(在没有设置lazy属性值为false),Hibernate只是从数据库获取符合条件的对象的OId从而生成代理对象,并没有加载出对象访 ...

  6. 【java IO】使用Java输入输出流 读取txt文件内数据,进行拼接后写入到另一个文件中

    package com.sxd.test.util; import java.io.BufferedReader; import java.io.BufferedWriter; import java ...

  7. hive脚本出现Error: java.lang.RuntimeException: Error in configuring object和Caused by: java.lang.IndexOutOfBoundsException: Index: 9, Size: 9

    是在reduce阶段报的错误,详细错误信息是 朱传豪 19:04:48 Diagnostic Messages for this Task: Error: java.lang.RuntimeExcep ...

  8. 【转】Kylin的cube模型

    转自:http://www.cnblogs.com/en-heng/p/5239311.html 1. 数据仓库的相关概念 OLAP 大部分数据库系统的主要任务是执行联机事务处理和查询处理,这种处理被 ...

  9. Angular JS 学习之 Scope作用域

    1.Scope作用域是应用在HTML(视图)和JavaScript(控制器)之间的纽带: Scope是一个对象,有可用的方法和属性: Scope可应用在视图和控制器上: 2.当你在AngularJS中 ...

  10. css3写箭头

    左箭头 .left-arrow { position: absolute; left: 6%; top: 31%; overflow: hidden; zoom:; width: 0.4rem; he ...