1. 程式人生 > >215. 數組中的第K個最大元素

215. 數組中的第K個最大元素

nbsp color top ges pub find pop largest bsp

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int, std::vector<int>, std::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(); } };

215. 數組中的第K個最大元素