It is an interview question:
Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB memory. Follow up with what you would do if you have only 10 MB of memory.
My analysis:
文件的大小
The size of the file is 4×109×4 bytes = 16 GB.
做排序可以很好的解决问题,只要找到排序范围外的整数即可。但是有没有除了排序更好的方法呢?
We can do external sorting, thus we get to know the range of the integers. My question is what is the best way to detect the missing integer in the sorted big integer sets?
My understanding(after reading all answers):
假设我们处理的是32位的整数,那么一共有2^32 约为 4*109 个不同的整数。
Assuming we are talking about 32-bit integers. There are 2^32 = 4*109 distinct integers.
有足够内存时可以使用位图,用每一位来表示一个整数。
Case 1: we have 1 GB = 1 * 109 * 8 bits = 8 billion bits memory. Solution: if we use one bit representing one distinct integer, it is enough. we don't need sort. Implementation:
回顾一下读文件:
作为C++风格的文件读取方式
可以使用文件流类——fstream类
fstream类有两种子类
分别是用于读出文件的ifstream类
以及用于写入文件ofstream类
在使用是应加入引用 : #include <fstream>
注意该头文件使用std命名空间
还应该加入语句 :using namespace std;
使用的使用应该创建一个文件流对象
比如读入一个文件可以使用下列语句:
ifstream File;
char *FileName;
char DataBuffer[128];
/* 此处应设定文件名 */
File.open(FileName); //打开文件
//open函数其实有三个参数,此处后两个使用默认值了,具体函数使用请见MSDN
if(File)
{ //文件打开成功
// 此处加入对文件内容的处理
while(!File.eof())
{ //循环读入数据
File.read(DataBuffer,128);
/*对缓冲区中的读入数据进行操作*/
}
}
else
{ //文件打开失败
/*进行错误处理*/
}
File.close(); //关闭文件
与上述代码类似
将内容写入文件需要创建一个ofstream对象
可以多看看MDSN
可以参考CPP标准函数库
int radix = 8;
byte[] bitfield = new byte[0xffffffff/radix];
void F() throws FileNotFoundException{
Scanner in = new Scanner(new FileReader("a.txt"));
while(in.hasNextInt()){
int n = in.nextInt();
bitfield[n/radix] |= (1 << (n%radix));
}
for(int i = 0; i< bitfield.lenght; i++){
for(int j =0; j<radix; j++){
if( (bitfield[i] & (1<<j)) == 0) System.out.print(i*radix+j);
}
}
}
对从文件中读出的整数进行处理的函数:
These functions use the constants to set, clear and test the value of a bit:
-
#define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F #define N 4000000000 int a[1 + N/BITSPERWORD];
一个整数a对32取模,即留下后5个bit:a&0x1F void set(int i) { a[i>>SHIFT] |= (1<<(i & MASK)); }//将整数对应的位置为1 void clr(int i) { a[i>>SHIFT] &= ~(1<<(i & MASK)); } int test(int i){ return a[i>>SHIFT] & (1<<(i & MASK)); }//测试整数对应的位
Case 2: 10 MB memory = 10 * 106 * 8 bits = 80 million bits
对于所有16bit的前缀,有65536种前缀,需要65536*4*8 = 2million bits。需要建立65536个桶。对于每一个桶,需要4bytes去存储所有可能的计数。因为最坏的情况是所有的数字都用一个桶计数。
Solution: For all possible 16-bit prefixes, there are 2^16 number of
integers = 65536, we need 2^16 * 4 * 8 = 2 million bits. We need build
65536 buckets. For each bucket, we need 4 bytes holding all possibilities because
the worst case is all the 4 billion integers belong to the same bucket.
桌上有十个苹果,要把这十个苹果放到九个抽屉里,无论怎样放,我们会发现至少会有一个抽屉里面放两个苹果。这一现象就是我们所说的“抽屉原理”。方法:增加计数器的值,读取计数器的值。
遍历文件,将每个整数的前16位对应的编号的桶中的计数器加1。
遍历每个桶,找出第一个计数器值小于65536的桶,记录此桶的编号,就是数组的下标,也就是缺失数字的前16bit了。
再次遍历数据文件,将数据的前16bit为上一步找出的数放入新的桶中。新的桶的编号为所找出的数的后16bit。
最后找出计数器为0的桶,桶的编号就为所缺的数的后16bit。
.......
或者最后一步可以使用bitmap,将拥有相同前16bit的数字的后16bit对应的数字用bitmap计数。
step1: Build the counter of each bucket through the first pass through the file.
step2: Scan the buckets, find the first one who has less than 65536 hit.
step3: Build new buckets whose high 16-bit prefixes are we found in step2
through second pass of the file
step4: Scan the buckets built in step3, find the first bucket which doesnt
have a hit.
The code is very similar to above one.
Conclusion: We decrease memory through increasing file pass.
http://stackoverflow.com/questions/7153659/find-an-integer-not-among-four-billion-given-ones
本文深入探讨了如何在内存限制条件下,寻找四亿整数集合中的缺失整数,包括使用位图法解决大规模整数集合问题及内存不足时的优化策略。
333

被折叠的 条评论
为什么被折叠?



