Skip to content

Practice Kth Largest Element in a Stream

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#703
LeetCode Problem

Kth Largest Element in a Stream

Problem Statement

Implement KthLargest(k, nums), initialized with rank k and the values in nums. add(val) appends val to the stream and returns its kth-largest value so far. Rank the complete multiset in descending order, so repeated equal values occupy separate positions; this is not the kth distinct value.

Source: Kth Largest Element in a Stream on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["KthLargest", "add", "add", "add", "add", "add"] [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
Output
[null, 4, 5, 5, 8, 8]

# Example 2

Input
["KthLargest", "add", "add", "add", "add"] [[4, [7, 7, 7, 7, 8, 3]], [2], [10], [9], [9]]
Output
[null, 7, 7, 7, 8]

Constraints

0 <= nums.length <= 10^4
1 <= k <= nums.length + 1
-10^4 <= nums[i] <= 10^4
-10^4 <= val <= 10^4
At most 10^4 calls will be made to add.

Related LeetCode Problems

Practice more problems similar to Kth Largest Element in a Stream.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.