- tags: Priority Queue,LeetCode101
Max heap priority queue
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
// max heap
priority_queue<int> pq;
for (auto iter = nums.begin(); iter != nums.end(); ++iter) {
pq.push(*iter);
}
// The Kth largest element should let k > 1 not k > 0
for (; k > 1; --k) {
pq.pop();
}
return pq.top();
}
};