Problem Statement:
You need to find the top K elements in a dynamic dataset where K keeps changing over time.
First Thought: Using a Heap
If
K
is fixed, a Min-Heap (priority queue) is a great choice.Time Complexity:
Insertion: O(log K)
Retrieval: O(K log K) (since extracting K elements requires sorting them)
Issue:
If
K
keeps changing, you need to rebuild the …