Skip to content

C++ Optimization for kth-largest-integer-in-a-stream #4791

@kavisubramanyan

Description

@kavisubramanyan

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions