# Leetcode 703 - Kth Largest Element in a Stream

`Note:`

- Seems like an easy question but what they are looking at is how to use
`heap`

. - Initialize a
`minHeap`

and add all nums. - While adding a new num, if
`heap.size > k`

, we need to pop it until the size becomes`k`

. - Because every time we are popping the
`min`

out of the heap. When the size reaches`k`

, the top of`minheap`

is our $k^{th}$ largest element.

`Question:`

Design a class to find the $k^{th}$ largest element in a stream. Note that it is the $k^{th}$ largest element in the sorted order, not the $k^{th}$ distinct element.

Implement KthLargest class:

`KthLargest(int k, int[] nums)`

Initializes the object with the integer k and the stream of integers nums.`int add(int val)`

Appends the integer val to the stream and returns the element representing the $k^{th}$ largest element in the stream.

`Example:`

1 | Input |

`Code:`

1 | /** |