LeetCode 703 Kth Largest Element in a Stream - Problem Statement

The problem

Design a class that finds the kth largest element in a stream.

KthLargest(k, nums): Initialize with k and an initial stream nums.

add(val): Add val to the stream and return the kth largest element.

With k = 3 and nums = [4, 5, 8, 2]:

  • Stream starts as [4, 5, 8, 2]. 3rd largest is 4.
  • add(3): stream is [4, 5, 8, 2, 3]. 3rd largest is 4.
  • add(5): stream is [4, 5, 8, 2, 3, 5]. 3rd largest is 5.
  • add(10): stream is [...]. 3rd largest is 5.

Constraints: 1k1041 \le k \le 10^4. Up to 10410^4 calls to add.