Interview question: Given a sequential file containing 4.3 billion 32-bit integers, find an integer that contains at least two times.
Idea: Consider two conditions
1. All integers are stored in the sequential file, so the number of times the file is read will significantly affect the efficiency of the algorithm
2. The number of integers contained in the sequential file is 4300000000, and if all reads are placed in memory, the memory space factor must be considered.
So, there is no time-saving and space-saving solution. That is, try to read only one file at a time and use as little memory as possible.
Solution:
By the above question, we think of Bit-map, can apply 537.5 million char array, each bit in the array corresponds to a number of 4.3 billion integers, at the beginning, all the bits are 0, if there is a corresponding number, then the corresponding bit is set one.
The question comes out again, how can I represent an integer that contains at least two times.
This is, we find that it is not enough to represent an integer that contains at least two times, only one. So with two bits. 00 means there is no data, 01 means there is one, 10 means there are two, 11 means there are more than two, over.
We need to apply an array of type char of size 1075000000, with two bits corresponding to one number.
Initially, all bits are zeroed, and then the sequential file is read, and after the integer is read, the corresponding bit is changed accordingly.
In this way, we only need one operation, and the least amount of memory is used to solve this problem.
Isn't it easy?