Http://zhedahht.blog.163.com/blog/static/2541117420072432136859/
Question: Enter n integers and output the smallest k integers.
For example, if you enter the 8 numbers 1, 2, 3, 5, 6, 7, and 8, the minimum four digits are 1, 2, 3, and 4.
Analysis: the simplest way to solve this problem is to sort the input n integers so that the number of K at the top is the minimum number of K. The time complexity of this idea is O (nLogN ). We try to find a faster solution.
We can first create a data container with the size of K to store the minimum K numbers. Next we will read a number from the input n integers each time. If the number in the container is less than K, put the integer read this time into the container. If there are K numbers in the container, that is, the container is full, at this time, we cannot insert a new number but can only replace an existing number. We can find the maximum value of the existing K number, and compare it with the integer to be inserted this time. If the value to be inserted is smaller than the current maximum value, replace the current maximum value with this number. If the value with insert is larger than the current maximum value, therefore, this number cannot be one of the smallest k integers, because we already have k numbers in our container smaller than it, so we can discard this integer.
Therefore, when the container is full, we need to do three things: one is to find the maximum number in K integers, and the other is to delete the maximum number in the container, 3. Insert a new number and ensure that K integers are still sorted. If we use a binary tree to implement this data container, we can (LogK) Implement these three steps in time. Therefore, for N input numbers, the total time efficiency is O (nLogK ).
We can use different binary trees to implement this data container. Since we need to find the largest number in K integers every time, we can easily think of using the largest heap. In the max heap, the value of the root node is always greater than the value of any node in its subtree. So we can get the maximum value of the existing K numbers in O (1) each time, but we need o (LogK) The deletion and insertion operations are completed in time.
We need some code to implement the largest heap from scratch. We can also use the red and black trees to implement our containers. The red-black tree divides nodes into red and black colors and ensures that the tree is balanced according to some rules. This ensures that only O (LogK ). In STL, set and Multiset are both implemented based on the red and black trees. If the interviewer is not opposed to using the data container in STL, we can use it directly. The following is the reference code based on STL et in STL:
typedef multiset<int, greater<int> > IntHeap;///////////////////////////////////////////////////////////////////////// find k least numbers in a vector///////////////////////////////////////////////////////////////////////void FindKLeastNumbers( const vector<int>& data, // a vector of data IntHeap& leastNumbers, // k least numbers, output unsigned int k ){ leastNumbers.clear(); if(k == 0 || data.size() < k) return; vector<int>::const_iterator iter = data.begin(); for(; iter != data.end(); ++ iter) { // if less than k numbers was inserted into leastNumbers if((leastNumbers.size()) < k) leastNumbers.insert(*iter); // leastNumbers contains k numbers and it's full now else { // first number in leastNumbers is the greatest one IntHeap::iterator iterFirst = leastNumbers.begin(); // if is less than the previous greatest number if(*iter < *(leastNumbers.begin())) { // replace the previous greatest number leastNumbers.erase(iterFirst); leastNumbers.insert(*iter); } } }}