Find the maximum number of K in N (requires complexity of O (n ))

Source: Internet
Author: User

First, we all know that we can create n elements as a maximum (large | small) Heap and O (n ).

The following common practice is to remove an element from the top of the heap and take it K times. Then, the first k elements are taken out. But the problem is that after each step removes an element, it takes the log (n) Time to maximize (large | small) the heap. So the complexity of taking K times is
Klog (n ). Is it possible to be lower?

After analyzing the above practice, we can find that there is a redundant operation: When an element is removed, we start to maximize the heap again. At this time, note that we do not need to adjust the log (n) layer further, because at most k elements are required, it is enough to adjust the K layer further, and the elements below will no longer work. Anyway, they will definitely be missing the first K. According to this method, you can optimize klog (n) to K (min (K,
Logn )).

But this is not enough. We can continue to optimize it. Why? In the above method, to ensure that all the potential first k elements are adjusted, we must adjust the K layer down, but in fact, from the final result, a large part of the adjustment may be in vain. After all, adjusting the K layer involves 2 ^ k elements, we can only take up to K of them.

Let's look at our needs from the most intuitive point of view: when the largest heap is built, we can imagine standing at the top of the heap and looking down, the above elements cover up the following elements, we only see the heap top element. First, remove the heap top element. Then the following two sons were revealed. We selected a big one from the two sons and revealed the two sons. Now we have three elements in our view, we selected the largest one from three, took it, and exposed its two sons ..
This keeps getting until K is full. This method should be optimal because it does not struggle to adjust irrelevant elements. Each time it is selected from the elements most likely to compete for the first K-why? According to our analysis last time, this method always compares the two elements that are most likely to be evenly matched, so that the information obtained is the largest.

The above is an intuitive model. To facilitate coding implementation, we must maintain "currently exposed elements" in one way ". Since each time we select the largest from the currently exposed elements, it is best to store them with the largest heap.

The complexity of this method is O (n + klogk), but it is more complexAlgorithmThe constant is much lower, so it should be very good in practice, especially when K is very small than N.

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.