Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
20 lines (20 sloc) 563 Bytes
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> heap;
int heapCount = 0;
for (vector<int>::iterator itr = nums.begin();
itr != nums.end(); ++itr) {
if (heapCount < k) {
heap.push(*itr);
heapCount++;
} else {
if (heap.top() < *itr) {
heap.pop();
heap.push(*itr);
}
}
}
return heap.top();
}
};