Data Structures and Algorithms: Top K Problems

The top K question is a question that is often tested in interviews, and it can often be solved by sorting (sorting) and heaps (big/small root heaps). Of course, if you can only queue quickly, you can finish the interview earlier and go home for dinner.

in conclusion:

The top K problem is often solved with a heap

1. Find the smallest k numbers in the array

topic:

Design an algorithm to find the smallest k numbers in the array. The k numbers can be returned in any order.

Example:

输入: arr = [1,3,5,7,2,4,6,8], k = 4
输出: [1,2,3,4]

1.1 Quick sort, O(nlogn), O(logn)

The easiest method to think of is quick sort, which has a time complexity of O(nlogn) and a space complexity of O(logn)

code:

class Solution:
    def smallestK(self, arr: List[int], k: int) -> List[int]:
        arr.sort()
        return arr[:k]

1.2 Big root heap, O(nlogk), O(k)

We use a large root heap to maintain the top k small values ​​of the array in real time. First insert the first k numbers into the big root heap, and then start traversing from the k+1th number. If the number currently traversed is smaller than the number at the top of the big root heap, pop the number at the top of the heap, and then insert the currently traversed number. Finally, store the number in the big root heap into the array and return it.

Since the heap created by the heapq library in Python is a small root heap, we need to take the opposite of all the numbers in the array to become a large root heap.

code:

class Solution:
    def smallestK(self, arr: List[int], k: int) -> List[int]:
        if not arr or k==0:
            return []
        heap=[-x for x in arr[:k]]
        heapq.heapify(heap)
        for x in arr[k:]:
            if -heap[0]>x:
                heapq.heappop(heap)
                heapq.heappush(heap,-x)
        return [-x for x in heap]

Complexity analysis:

  • Time complexity: O(nlogk), where n is the length of the array arr. Since the big root heap maintains the first k small values ​​in real time, the time complexity of insertion and deletion is O(logk). In the worst case, n numbers in the array will be inserted, so a total time complexity of O(nlogk) is required.
  • Space complexity: O(k), because there are at most k numbers in the big root heap

Related topics are being updated continuously...

Guess you like

Origin blog.csdn.net/qq_43799400/article/details/131753582