# MaxHeap

class MaxHeap {
  constructor(nums) {
    this.heap = nums;
    // 从最低非根节点往上排序
    for (let i = this.getParentIndex(nums.length); i >= 0; i--) {
      this.shiftUp(i);
    }
  }

  getParentIndex(i) {
    return Math.floor(i / 2) + 1;
  }
  getLeftIndex(i) {
    return i * 2 + 1;
  }
  getRightIndex(i) {
    return i * 2 + 2;
  }
  shiftUp(i) {
    const left = this.getLeftIndex(i);
    const right = this.getRightIndex(i);
    const length = this.size();
    let target = i;
    if (left < length && this.heap[left] > this.heap[target]) {
      target = left;
    }
    if (right < length && this.heap[right] > this.heap[target]) {
      target = right;
    }
    if (target !== i) {
      this.move(target, i);
      this.shiftUp(target);
    }
  }
  move(i, j) {
    const temp = this.heap[i];
    this.heap[i] = this.heap[j];
    this.heap[j] = temp;
  }
  shift() {
    this.heap[0] = this.heap.pop();
    this.shiftUp(0);
  }
  size() {
    return this.heap.length;
  }
  peek() {
    return this.heap[0];
  }
}

var findKthLargest = function(nums, k) {
  const heap = new MaxHeap(nums);
  console.log(heap);
  for (i = 0; i < k - 1; i++) {
    heap.shift();
  }

  return heap.peek();
};

console.log(findKthLargest([3, 2, 1, 5, 6, 4], 2));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Last Updated: 6/3/2024, 6:52:00 PM