priority_queue with O(n * log(n - k))


  • 0
    B

    int findKthLargest(vector<int>& nums, int k) {
    priority_queue<int> data;
    int pos = nums.size() - k;
    int result = INT_MAX;

        for (auto it : nums)
        {
            data.push(it);
            
            if (data.size() > pos)
            {
                result = min(result, data.top());
                data.pop();
            }
        }
        
        
        return result;
    }

Log in to reply
 

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