mirror of
https://gitee.com/TheAlgorithms/LeetCodeAnimation.git
synced 2024-12-06 15:19:44 +08:00
19 lines
439 B
C++
19 lines
439 B
C++
#include <queue>
|
|
|
|
class Solution {
|
|
public:
|
|
int findKthLargest(vector<int>& nums, int k) {
|
|
priority_queue<int, vector<int>, greater<int> > Q;
|
|
for(int i = 0; i < nums.size(); i++){
|
|
if(Q.size() < k){
|
|
Q.push(nums[i]);
|
|
}
|
|
else if(Q.top() < nums[i]){
|
|
Q.pop();
|
|
Q.push(nums[i]);
|
|
}
|
|
}
|
|
|
|
return Q.top();
|
|
}
|
|
}; |