Kth Largest Element in a Stream

Difficulty: Easy

Design a class that tracks the kth largest element in a collection of numbers that arrive one at a time.

The constructor takes an integer k and an initial array of numbers. add(val) adds val and returns the kth largest element so far. Duplicates count: it's the kth largest, not the kth distinct value.

There will always be at least k numbers when add is called.

Examples

Input: k = 3, nums = [4, 5, 8, 2]; then add(3), add(5), add(10), add(9), add(4)
Output: 4, 5, 5, 8, 8

Starting from [4,5,8,2], the 3 largest are 8,5,4, so the 3rd largest is 4. After add(3), the collection is [4,5,8,2,3] and the 3rd largest is still 4. After add(5) it becomes 5, and so on as more numbers arrive.

Constraints

  • 1 <= k <= 10^4

  • 0 <= nums.length <= 10^4

  • -10^4 <= nums[i] <= 10^4

  • At most 10^4 calls to add

Approach

Re-sorting on every call re-examines numbers that will never be in the top k. Instead, keep only the k largest values seen so far.

A min-heap of size k fits this well. It holds the k largest values, and its top (the smallest of those k) is the kth largest overall. When a value arrives, add it. If the heap grows past size k, remove its minimum, which is no longer in the top k. The top is the answer.

Solutions

Brute Force - Sort on Every Call

Keep all numbers in an array. On each add(), push the value, sort the array, and read the kth largest by index.

class KthLargest {
  constructor(k, nums) {
    this.k = k;
    this.nums = [...nums];
  }

  add(val) {
    this.nums.push(val);
    this.nums.sort((a, b) => b - a);
    return this.nums[this.k - 1];
  }
}

Time: O(n log n) per add call, where n is the number of values seen so far · Space: O(n) to store every value ever added

Optimal - Min-Heap of Size k

Keep a min-heap of only the k largest values so far. Its top, the smallest of those k, is the kth largest overall.

class MinHeap {
  constructor() {
    this.data = [];
  }

  size() {
    return this.data.length;
  }

  peek() {
    return this.data[0];
  }

  push(val) {
    this.data.push(val);
    let i = this.data.length - 1;
    while (i > 0) {
      const parent = (i - 1) >> 1;
      if (this.data[i] < this.data[parent]) {
        [this.data[i], this.data[parent]] = [this.data[parent], this.data[i]];
        i = parent;
      } else break;
    }
  }

  pop() {
    const top = this.data[0];
    const last = this.data.pop();
    if (this.data.length > 0) {
      this.data[0] = last;
      let i = 0;
      const n = this.data.length;
      while (true) {
        let smallest = i;
        const left = 2 * i + 1;
        const right = 2 * i + 2;
        if (left < n && this.data[left] < this.data[smallest]) smallest = left;
        if (right < n && this.data[right] < this.data[smallest]) smallest = right;
        if (smallest === i) break;
        [this.data[i], this.data[smallest]] = [this.data[smallest], this.data[i]];
        i = smallest;
      }
    }
    return top;
  }
}

class KthLargest {
  constructor(k, nums) {
    this.k = k;
    this.heap = new MinHeap();
    for (const num of nums) this.add(num);
  }

  add(val) {
    this.heap.push(val);
    if (this.heap.size() > this.k) this.heap.pop();
    return this.heap.peek();
  }
}

Time: O(log k) per add call · Space: O(k) - the heap never grows past size k