I know that we can find the k-largest numbers from n unsorted integers in 2 ways:
Suppose the n integers are in a stream and we don't have random access to them
I want to know is it possible to find the k-largest numbers from n unsorted integers with time complexity O(n) and space complexity O(k)?
It is. After filling the heap with k elements, instead of evicting one element from the heap after every insertion, evict k elements from the heap after every k insertions. Then you don't need the heap structure any more -- just select every time.
k次冒泡排序将在数组timeo(nk)的最后给出k个最大元素
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.