comments | difficulty | edit_url | tags | ||||||
---|---|---|---|---|---|---|---|---|---|
true |
简单 |
|
设计一个找到数据流中第 k
大元素的类(class)。注意是排序后的第 k
大元素,不是第 k
个不同的元素。
请实现 KthLargest
类:
KthLargest(int k, int[] nums)
使用整数k
和整数流nums
初始化对象。int add(int val)
将val
插入数据流nums
后,返回当前数据流中第k
大的元素。
示例 1:
输入:
["KthLargest", "add", "add", "add", "add", "add"]
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
输出:[null, 4, 5, 5, 8, 8]
解释:
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
kthLargest.add(3); // 返回 4
kthLargest.add(5); // 返回 5
kthLargest.add(10); // 返回 5
kthLargest.add(9); // 返回 8
kthLargest.add(4); // 返回 8
示例 2:
输入:
["KthLargest", "add", "add", "add", "add"]
[[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
输出:[null, 7, 7, 7, 8]
解释:
KthLargest kthLargest = new KthLargest(4, [7, 7, 7, 7, 8, 3]);kthLargest.add(2); // 返回 7
kthLargest.add(10); // 返回 7
kthLargest.add(9); // 返回 7
kthLargest.add(9); // 返回 8
提示:
0 <= nums.length <= 104
1 <= k <= nums.length + 1
-104 <= nums[i] <= 104
-104 <= val <= 104
- 最多调用
add
方法104
次
我们维护一个优先队列(小根堆)$\textit{minQ}$。
初始化时,我们将数组
每次加入一个新元素时,如果
这样,$\textit{minQ}$ 中的元素就是数组
空间复杂度
class KthLargest:
def __init__(self, k: int, nums: List[int]):
self.k = k
self.min_q = []
for x in nums:
self.add(x)
def add(self, val: int) -> int:
heappush(self.min_q, val)
if len(self.min_q) > self.k:
heappop(self.min_q)
return self.min_q[0]
# Your KthLargest object will be instantiated and called as such:
# obj = KthLargest(k, nums)
# param_1 = obj.add(val)
class KthLargest {
private PriorityQueue<Integer> minQ;
private int k;
public KthLargest(int k, int[] nums) {
this.k = k;
minQ = new PriorityQueue<>(k);
for (int x : nums) {
add(x);
}
}
public int add(int val) {
minQ.offer(val);
if (minQ.size() > k) {
minQ.poll();
}
return minQ.peek();
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest obj = new KthLargest(k, nums);
* int param_1 = obj.add(val);
*/
class KthLargest {
public:
KthLargest(int k, vector<int>& nums) {
this->k = k;
for (int x : nums) {
add(x);
}
}
int add(int val) {
minQ.push(val);
if (minQ.size() > k) {
minQ.pop();
}
return minQ.top();
}
private:
int k;
priority_queue<int, vector<int>, greater<int>> minQ;
};
/**
* Your KthLargest object will be instantiated and called as such:
* KthLargest* obj = new KthLargest(k, nums);
* int param_1 = obj->add(val);
*/
type KthLargest struct {
k int
minQ hp
}
func Constructor(k int, nums []int) KthLargest {
minQ := hp{}
this := KthLargest{k, minQ}
for _, x := range nums {
this.Add(x)
}
return this
}
func (this *KthLargest) Add(val int) int {
heap.Push(&this.minQ, val)
if this.minQ.Len() > this.k {
heap.Pop(&this.minQ)
}
return this.minQ.IntSlice[0]
}
type hp struct{ sort.IntSlice }
func (h *hp) Less(i, j int) bool { return h.IntSlice[i] < h.IntSlice[j] }
func (h *hp) Pop() interface{} {
old := h.IntSlice
n := len(old)
x := old[n-1]
h.IntSlice = old[0 : n-1]
return x
}
func (h *hp) Push(x interface{}) {
h.IntSlice = append(h.IntSlice, x.(int))
}
/**
* Your KthLargest object will be instantiated and called as such:
* obj := Constructor(k, nums);
* param_1 := obj.Add(val);
*/
class KthLargest {
#k: number = 0;
#minQ = new MinPriorityQueue();
constructor(k: number, nums: number[]) {
this.#k = k;
for (const x of nums) {
this.add(x);
}
}
add(val: number): number {
this.#minQ.enqueue(val);
if (this.#minQ.size() > this.#k) {
this.#minQ.dequeue();
}
return this.#minQ.front().element;
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* var obj = new KthLargest(k, nums)
* var param_1 = obj.add(val)
*/
/**
* @param {number} k
* @param {number[]} nums
*/
var KthLargest = function (k, nums) {
this.k = k;
this.minQ = new MinPriorityQueue();
for (const x of nums) {
this.add(x);
}
};
/**
* @param {number} val
* @return {number}
*/
KthLargest.prototype.add = function (val) {
this.minQ.enqueue(val);
if (this.minQ.size() > this.k) {
this.minQ.dequeue();
}
return this.minQ.front().element;
};
/**
* Your KthLargest object will be instantiated and called as such:
* var obj = new KthLargest(k, nums)
* var param_1 = obj.add(val)
*/