Priority_queue as heap c++


  • 0
    K
    class compare{
    public:
    bool operator()(const int & a, const int & b)
    {
        return a>b;
    }
    
       };
        class Solution {
         public:
          int findKthLargest(vector<int>& nums, int k) {
           priority_queue<int, vector<int>,compare>queue;
           for(int i=0;i<nums.size();i++)
          {
             if(queue.size()<k)queue.push(nums[i]);
                else if(nums[i]>queue.top()){
                  queue.pop();
                   queue.push(nums[i]);
             }
        }
        return queue.top();
    }
    

    };


Log in to reply
 

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