Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 628 Bytes

215_kthLargestElementInAnArray.md

File metadata and controls

28 lines (22 loc) · 628 Bytes

Sorting

  • sort the array and return n-kth element.

Priority Queue

  • push all elements into a priority queue and pop k-1 elements from it.
  • the top of the priority queue is the k-th largest element.

Code

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int> pq;
        for(auto x:nums){
            pq.push(x);
        }
        while(--k){
            pq.pop();
        }
        return pq.top();
    }
};