703. Kth Largest Element in a Stream

Posted jtechroad

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
 */

 

以上是关于703. Kth Largest Element in a Stream的主要内容,如果未能解决你的问题,请参考以下文章

703. Kth Largest Element in a Stream

703. Kth Largest Element in a Stream

Leetcode 703题数据流中的第K大元素(Kth Largest Element in a Stream)Java语言求解

leetcode Kth Largest Element in a Stream——要熟悉heapq使用

LeetCode算法题-Kth Largest Element in a Stream(Java实现)

Kth Largest Element