class KthLargest {
public:
    KthLargest(int k, vector<int> nums) {
        size = k;
        for(auto num:nums){
            pq.push(num);
            if(pq.size() > size)
                pq.pop();
        }
    }

    int add(int val) {
        pq.push(val);
        if(pq.size() > size)
            pq.pop();
        return pq.top();
    }
private:
    priority_queue<int, vector<int>, greater<int>> pq;
    int size;
};

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

 

posted on 2018-10-01 16:10  Sempron2800+  阅读(110)  评论(0编辑  收藏  举报