[CareerCup] 18.6 Smallest One Million Numbers 最小的一百万个数字
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] 18.6 Smallest One Million Numbers 最小的一百万个数字的更多相关文章
- [CareerCup] 18.1 Add Two Numbers 两数相加
18.1 Write a function that adds two numbers. You should not use + or any arithmetic operators. 这道题让我 ...
- [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 ...
- [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 ...
- [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 ...
- [CareerCup] 18.11 Maximum Subsquare 最大子方形
18.11 Imagine you have a square matrix, where each cell (pixel) is either black or white. Design an ...
- [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 ...
- [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 ...
- [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 ...
- [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出现的个数,比如如 ...
随机推荐
- 配置ogg异构oracle-mysql(3)目的端配置
目的端配置大致分为如下三个步骤:配置mgr,配置checkpoint table,配置应用进程 在目的端先创建一张表,记得带主键: mysql> create database hr;Query ...
- c语言运算符号的优先级
c语言运算符号的优先级 本文来自百度搜索只为查看方便 优先级等级口诀: 圆方括号.箭头一句号, 自增自减非反负.针强地址长度, 乘除,加减,再移位, 小等大等.等等不等, 八位与,七位异,六位或,五与 ...
- JMockit
[TOC] 简介 JMockit是基于JavaSE5中的java.lang.instrument包开发,内部使用ASM库来动态修改java的字节码,使得java这种静态语言可以想动态脚本语言一样动态设 ...
- APP设计尺寸规范大全,APP界面设计新手教程【官方版】(转)
正值25学堂一周年之际,同时站长和APP设计同仁们在群里(APP界面设计 UI设计交流群,APP界面设计⑥群 APPUI设计③群58946771 APP设计资源⑤群 386032923欢迎大家加入交流 ...
- 转一篇dudu大人的文章:程序员,用NuGet管理好你的包包
每个女人都有很多包包:其实男人也有,但只有会写程序的男人才有 —— 代码世界中的大“包”小“包”.这些大包小包,有花钱买的,有从开源市场淘的,也有自己或同事亲手制作的. 包包有个特点:容易坏,更新快, ...
- java中static作用详解
static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...
- 【POI xls Java map】使用POI处理xls 抽取出异常信息 --java1.8Group by ---map迭代 -- 设置单元格高度
代码处理逻辑: 代码流程: 1.首先需要创建一个实体 用来存储 相关信息 package com.sxd.test.unusualName; public class NameEntity { pri ...
- Linux常用工具之XFTP、Xshell配置
Xftp是一个基于 MS windows 平台的功能强大的SFTP.FTP文件传输软件.使用了 Xftp 以后,MS windows 用户能安全地在UNIX/Linux 和 Windows PC 之间 ...
- JavaScript笔试必备语句
1. document.write( " "); 输出语句 2.JS中的注释为// 3.传统的HTML文档顺序是:document- >html- >(head,bod ...
- spring优化使用
1.bean由框架填充,避免手写优化代码. 2.view的展示通过配置或注解实现最优化使用架构. 待续...