-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Open
Description
Optimization for https://neetcode.io/problems/kth-largest-integer-in-a-stream
I noticed that Heapify is listed as a prerequisite for kth-largest-integer-in-a-stream. The current C++ solution populates the heap number by number:
for (int num : nums) {
minHeap.push(num);
if (minHeap.size() > k) {
minHeap.pop();
}
}
This populates the heap in O(nlogn). I'm proposing adding an [Optimal] solution, like those present for other questions that uses range construction in O(n) time. This is supported in C++ via Heapify when constructing from a vector:
priority_queue<int, vector<int>, greater<int>> min_heap(nums.begin(), nums.end());
Reference: https://en.cppreference.com/w/cpp/container/priority_queue/priority_queue
Happy to work on this as well.
Metadata
Metadata
Assignees
Labels
No labels