bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 703. Kth Largest Element in a Stream

703. Kth Largest Element in a Stream

Leetcode Solutions

Key approach of the solution using a Min-Heap

  1. Initialize the min-heap with the elements from the input array nums.
  2. If the size of the heap is greater than k, remove elements until the size is exactly k.
  3. For each add operation, insert the new value val into the heap if the heap size is less than k or if val is greater than the root of the heap.
  4. If the heap size exceeds k after insertion, remove the root of the heap.
  5. Return the root of the heap as the kth largest element.
UML Thumbnail

Key approach of the solution using Sorting

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR