-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKthLargestElementInAStream.ts
135 lines (123 loc) · 3.75 KB
/
KthLargestElementInAStream.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Source : https://leetcode.com/problems/kth-largest-element-in-a-stream/
// Author : squxq
// Date : 2023-11-04
/*****************************************************************************************************
*
* 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
* ["KthLargest", "add", "add", "add", "add", "add"]
* [[3, [4, 5, 8, 2]], [3], [5], [10], [9], [4]]
* Output
* [null, 4, 5, 5, 8, 8]
*
* Explanation
* KthLargest kthLargest = new KthLargest(3, [4, 5, 8, 2]);
* kthLargest.add(3); // return 4
* kthLargest.add(5); // return 5
* kthLargest.add(10); // return 5
* kthLargest.add(9); // return 8
* kthLargest.add(4); // return 8
*
* Constraints:
*
* 1 <= k <= 10^4
* 0 <= nums.length <= 10^4
* -10^4 <= nums[i] <= 10^4
* -10^4 <= val <= 10^4
* At most 10^4 calls will be made to add.
* It is guaranteed that there will be at least k elements in the array when you search for
* the k^th element.
******************************************************************************************************/
/** Previous submit:
class KthLargest {
largest: number
elements: number[]
constructor(k: number, nums: number[]) {
this.largest = k
this.elements = nums
}
add(val: number): number {
this.elements.push(val)
this.elements.sort((a, b) => a - b)
return this.elements[this.elements.length - this.largest]
}
}
*/
export class KthLargest {
k: number;
heap: number[];
constructor(k: number, nums: number[]) {
this.k = k;
this.heap = [];
for (const num of nums) {
this.add(num);
}
}
add(val: number): number {
if (this.heap.length < this.k) {
this.heap.push(val);
this.heapifyUp(this.heap.length - 1);
} else if (val > (this.heap[0] as number)) {
this.heap[0] = val;
this.heapifyDown(0);
}
return this.heap[0] as number;
}
private heapifyUp(index: number): void {
// get parent index of current element
const parentIndex = Math.floor((index - 1) / 2);
// compare the current element with its parent
// if its smaller, swap them and continue heapifying
if (
parentIndex >= 0 &&
(this.heap[index] as number) < (this.heap[parentIndex] as number)
) {
[this.heap[index], this.heap[parentIndex]] = [
this.heap[parentIndex] as number,
this.heap[index] as number,
];
this.heapifyUp(parentIndex);
}
}
private heapifyDown(index: number): void {
const leftChildIndex = 2 * index + 1;
const rightChildIndex = 2 * index + 2;
let smallest: number = index;
// find the index of the smallest child (if smaller than current element)
if (
leftChildIndex < this.heap.length &&
(this.heap[leftChildIndex] as number) < (this.heap[smallest] as number)
) {
smallest = leftChildIndex;
}
if (
rightChildIndex < this.heap.length &&
(this.heap[rightChildIndex] as number) < (this.heap[smallest] as number)
) {
smallest = rightChildIndex;
}
if (smallest !== index) {
[this.heap[index], this.heap[smallest]] = [
this.heap[smallest] as number,
this.heap[index] as number,
];
this.heapifyDown(smallest);
}
}
}
/**
* Your KthLargest object will be instantiated and called as such:
* var obj = new KthLargest(k, nums)
* var param_1 = obj.add(val)
*/