Minimum K count

Source: Internet
Author: User

Problem description: The given n integers calculate the minimum K number.
The most intuitive solution is to sort n numbers in ascending order and then output the first k. However, in terms of efficiency, this method is not the best. An improved method is to use the division of arrays in quick sorting to divide arrays with k elements so that numbers smaller than k numbers are on the left, its larger number is on its right.
[Cpp]
Void Swap (int & a, int & B)
{
Int c = a; a = B; B = c;
}
 
Int Partition (int data [], int length, int start, int end)
{
If (data = NULL | length <= 0)
Return-1;
 
Int index = start-1;
For (int I = start; I <end; ++ I)
{
If (data [I] <data [end])
{
++ Index;
Swap (data [I], data [index]);
}
}
++ Index;
Swap (data [index], data [end]);
Return index;
}
 
Void GetKLeastNumbers (int data [], int length, int result [], int k)
{
If (data = NULL | length <= 0 | result = NULL | k <= 0)
Return;
 
Int start = 0, end = length-1;
Int index = Partition (data, length, start, end );
 
While (index! = K-1) // The k number is used as the basis for Array Division
{
If (index> k-1)
Index = Partition (data, length, start, index-1 );
Else
Index = Partition (data, length, index + 1, end );
}
For (int I = 0; I <k; ++ I)
Result [I] = data [I];
}
Through analysis, we can determine that the time complexity of the algorithm is O (n), which is a relatively efficient solution. However, the problem with the above algorithm is that the original array data is modified, so the application will be restricted if the original data cannot be modified.
Without modifying the raw data, we can create a container with a size of k to store the minimum k. Then, we traverse n integers. If the number in the container is less than k, we directly store the read number into the container. If the number in the container is greater than or equal to k, at the same time, if the number currently read is smaller than the maximum number in the container, delete the maximum number in the container and read the number into the container. Otherwise, no operation is performed. To quickly delete the maximum number of containers, you can consider using the maximum heap for storing container data. Because the value of the root node of the maximum heap is greater than the value of any node in its subtree, you can obtain the number of existing k vertices in O (1, the deletion and insertion time is O (lgk ). The total algorithm time complexity for insert and delete operations on n repeated Max heap is O (nlgk ). The following is the implementation code of the above algorithm using STL multiset.
[Cpp]
// Sets the minimum number of k Records for storing the maximum heap.
Void GetKLeastNumbers (const int data [], int length, multiset <int, greater <int> & result, int k)
{
If (data = NULL | k <1 | k> length) // determine the data Validity
Return;
 
Result. clear ();
Multiset <int, greater <int> >:: iterator iter;
For (int I = 0; I <length; ++ I)
{
If (result. size () <k)
Result. insert (data [I]);
Else
{
Iter = result. begin ();
If (* iter <* (result. begin ()))
{
Result. erase (iter );
Result. insert (data [I]);
}
}
}
}

Both of the above methods achieve the minimum k number. Although the second method is slower than the first method, it does not modify the original data, and is more suitable for massive data processing. Therefore, the two methods have their own advantages and disadvantages. in actual application, the algorithm selection is determined based on the actual situation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.