mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
46 lines
1.8 KiB
HTML
46 lines
1.8 KiB
HTML
|
<p>设计一个找到数据流中第 <code>k</code> 大元素的类(class)。注意是排序后的第 <code>k</code> 大元素,不是第 <code>k</code> 个不同的元素。</p>
|
|||
|
|
|||
|
<p>请实现 <code>KthLargest</code> 类:</p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>KthLargest(int k, int[] nums)</code> 使用整数 <code>k</code> 和整数流 <code>nums</code> 初始化对象。</li>
|
|||
|
<li><code>int add(int val)</code> 将 <code>val</code> 插入数据流 <code>nums</code> 后,返回当前数据流中第 <code>k</code> 大的元素。</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>示例:</strong></p>
|
|||
|
|
|||
|
<pre>
|
|||
|
<strong>输入:</strong>
|
|||
|
["KthLargest", "add", "add", "add", "add", "add"]
|
|||
|
[[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
|
|||
|
<strong>输出:</strong>
|
|||
|
[null, 4, 5, 5, 8, 8]
|
|||
|
|
|||
|
<strong>解释:</strong>
|
|||
|
KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
|
|||
|
kthLargest.add(3); // return 4
|
|||
|
kthLargest.add(5); // return 5
|
|||
|
kthLargest.add(10); // return 5
|
|||
|
kthLargest.add(9); // return 8
|
|||
|
kthLargest.add(4); // return 8
|
|||
|
</pre>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><strong>提示:</strong></p>
|
|||
|
|
|||
|
<ul>
|
|||
|
<li><code>1 <= k <= 10<sup>4</sup></code></li>
|
|||
|
<li><code>0 <= nums.length <= 10<sup>4</sup></code></li>
|
|||
|
<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>
|
|||
|
<li><code>-10<sup>4</sup> <= val <= 10<sup>4</sup></code></li>
|
|||
|
<li>最多调用 <code>add</code> 方法 <code>10<sup>4</sup></code> 次</li>
|
|||
|
<li>题目数据保证,在查找第 <code>k</code> 大元素时,数组中至少有 <code>k</code> 个元素</li>
|
|||
|
</ul>
|
|||
|
|
|||
|
<p> </p>
|
|||
|
|
|||
|
<p><meta charset="UTF-8" />注意:本题与主站 703 题相同: <a href="https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/">https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/</a></p>
|