1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/数据流中的第 K 大元素 [jBjn9C].html

46 lines
1.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>设计一个找到数据流中第 <code>k</code> 大元素的类class。注意是排序后的第 <code>k</code> 大元素,不是第 <code>k</code> 个不同的元素。</p>
<p>请实现 <code>KthLargest</code>&nbsp;类:</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>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>
<strong>输入:</strong>
[&quot;KthLargest&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;, &quot;add&quot;]
[[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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= k &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<li><code>-10<sup>4</sup> &lt;= val &lt;= 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>&nbsp;</p>
<p><meta charset="UTF-8" />注意:本题与主站 703&nbsp;题相同:&nbsp;<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>