【Leetcode_easy】703. Kth Largest Element in a Stream

problem

703. Kth Largest Element in a Stream

题意:

solution1:

priority_queue这个类型没有看明白。。。

class KthLargest {
public:
    KthLargest(int k, vector<int>& nums) {
        for(int num:nums)
        {
            q.push(num);
            if(q.size()>k) q.pop();
        }
        K = k;
    }
    
    int add(int val) {
        q.push(val);
        if(q.size()>K) q.pop();
        return q.top();
    }
private:
    priority_queue<int, vector<int>, greater<int>> q;//err...
    int K;
};

/**
 * Your KthLargest object will be instantiated and called as such:
 * KthLargest* obj = new KthLargest(k, nums);
 * int param_1 = obj->add(val);
 */

参考

1. Leetcode_easy_703. Kth Largest Element in a Stream;

2. Grandyang;

原文地址:https://www.cnblogs.com/happyamyhope/p/11091460.html