zoukankan      html  css  js  c++  java
  • 703. Kth Largest Element in a Stream

    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);
     */
  • 相关阅读:
    SEO搜索引擎
    SEO
    编程的智慧
    ES6编程规范
    面试题集
    motto
    motto
    JS
    motto
    Linux
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/9977760.html
Copyright © 2011-2022 走看看