18.6 Describe an algorithm to find the smallest one million numbers in one billion numbers. Assume that the computer memory can hold all one billion numbers.

这道题让我们在十亿个数字中找到最小的一百万个数字,而且限定了计算机只有能存十亿个数字的内存。这题有三种解法,排序,最小堆,和选择排序。

首先来看排序方法,这种方法简单明了,就是把这十亿个数字按升序排列,然后返回前一百万个即可,时间复杂度是O(nlgn)。

然后来看最小堆做法,我们建立一个最大堆(大的数字在顶端),然后将前一百万个数字加进去。然后我们开始遍历剩下的数字,对于每一个数字,我们将其加入堆中,然后删掉堆中最大的数字。遍历接受后,我们就有了一百万个最小的数字,时间复杂度是O(nlgm),其中m是我们需要找的数字个数。

最后我们来看选择排序的方法,这种方法可以在线性时间内找到第i个最大或最小的数,如果数字都不是不同的,那么我们可以在O(n)的时间内找到第i个最小的数字,算法如下:

1. 随机选取数组中的一个数字当做pivot,然后以此来分割数组,记录分割处左边的数字的个数。

2. 如果左边正好有i个数字,那么返回左边最大的数字。

3. 如果左边数字个数大于i,那么继续在左边递归调用这个方法。

4. 如果左边数字个数小于i,那么在右边递归调用这个方法,但是此时的rank变为i - left_size。

参见代码如下:

int partition(vector<int> &array, int left, int right, int pivot) {
while (true) {
while (left <= right && array[left] <= pivot) ++left;
while (left <= right && array[right] > pivot) --right;
if (left >right) return left - ;
swap(array[left], array[right]);
}
} int find_max(vector<int> &array, int left, int right) {
int res = INT_MIN;
for (int i = left; i <= right; ++i) {
res = max(res, array[i]);
}
return res;
} int selection_rank(vector<int> &array, int left, int right, int rank) {
int pivot = array[rand() % (right - left + ) + left];
int left_end = partition(array, left, right, pivot);
int left_size = left_end - left + ;
if (left_size == rank + ) return find_max(array, left, left_end);
else if (rank < left_size) return selection_rank(array, left, left_end, rank);
else return selection_rank(array, left_end + , right, rank - left_size);
}

一旦找到了第i个最小的数字后,就可以遍历整个数组来找所有小于等于该数字的元素。当数组有重复元素的话,需要修改一些地方,但是时间就不能保证是线性的了。其实也有算法能线性时间内处理有重复的数组,但是比较复杂,有兴趣的请自行搜索研究。

CareerCup All in One 题目汇总

[CareerCup] 18.6 Smallest One Million Numbers 最小的一百万个数字的更多相关文章

  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.9 Find and Maintain the Median Value 寻找和维护中位数

    18.9 Numbers are randomly generated and passed to a method. Write a program to find and maintain the ...

  3. [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字

    You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...

  4. [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 ...

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

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

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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出现的个数,比如如 ...

随机推荐

  1. Android学习网站推荐(转)

    收集了一些比较好的Android学习网站,希望对大家有所帮助: 1.http://developer.android.com/ Android官方网站,可惜被屏蔽了,需要使用FQ软件 2.http:/ ...

  2. Android SDK Manager 中如果没有相应的镜像ARM XX Image

    Android SDK Manager 中如果没有相应的镜像ARM XX Image 处理做法是:先更新 相应版本Android SDK Tools 然后出现 ARM XX Image

  3. python web编程 创建一个web服务器

    这里就介绍几个底层的用于创建web服务器的模块,其中最为主要的就是BaseHTTPServer,很多框架和web服务器就是在他们的基础上创建的 基础知识 要建立一个Web 服务,一个基本的服务器和一个 ...

  4. python客户端编程

    上一篇说了最为底层的用来网络通讯的套接字.有很多基于套接字的一些协议,这些协议构成了当今互联网大多数客户服务器应用的核心 其实这些协议时在套接字上面的进一层封装用来完成特定的应用,这些应用主要包括: ...

  5. util.js

    轻量基础库.方法库 用时可直接拷贝 拆卸式使用 适用于mobile端简单页面 适用于PC简单页面 基于node.php等多种构建方法 (function(M){ /** * 初始化Ajax请求 * @ ...

  6. [转载]C/C++可变参数之va_start和va_end使用详解

    本文主要介绍va_start和va_end的使用及原理. 在以前的一篇帖子Format MessageBox 详解中曾使用到va_start和va_end这两个宏,但对它们也只是泛泛的了解. 介绍这两 ...

  7. 开发Portlet第三步:如何在Crystal Portlet中调用远程服务?

    当基于测试数据的Portlet调试完成后,接下来就是引入远程服务,替换测试数据. (此处以Dubbo框架远程服务为例) 分步指南 删除测试数据依赖:在pom.xml文件中,删除对****-servic ...

  8. 如何创建一个Android项目

    第一步: File -->New ---->Android Application Project 点击创建 第二步:接下来是几个下拉选择框. Minimum Required SDK 是 ...

  9. SpringHttpInvoker解析3-客户端实现

    主要的配置文件 <bean id="httpInvokerUserService" class="org.springframework.remoting.http ...

  10. poj2796 维护区间栈//单调栈

    http://poj.org/problem?id=2796 题意:给你一段区间,需要你求出(在这段区间之类的最小值*这段区间所有元素之和)的最大值...... 例如: 6 3 1 6 4 5 2 以 ...