C++ nlogk solution with priority queue


  • 0
    P
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int, vector<int>, greater<int>> heap;
        for (int n : nums) {
            if (heap.size() < k) heap.push(n);
            else if (heap.top() < n) {
                heap.pop();
                heap.push(n);
            }
        }
        return heap.top();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.