码迷,mamicode.com
首页 > 其他好文 > 详细

703. Kth Largest Element in a Stream

时间:2018-11-18 15:07:25      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:this   auto   obj   lse   ble   col   size   param   .com   

https://leetcode.com/problems/kth-largest-element-in-a-stream/description/

class KthLargest {
public:
    priority_queue<int> q;
    int k;
    KthLargest(int k, vector<int> nums) {
        this->k = k;
        for (auto &i : nums)
            add(i);
    }
    
    int add(int val) {
        if (q.size() < k) {
            q.push(-val);
        }
        else {
            if (val > -q.top()) {
                q.pop();
                q.push(-val);
            }
        }
        return -q.top();
    }
};

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

 

703. Kth Largest Element in a Stream

标签:this   auto   obj   lse   ble   col   size   param   .com   

原文地址:https://www.cnblogs.com/JTechRoad/p/9977760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!