-
Notifications
You must be signed in to change notification settings - Fork 2
/
findKthLargest.ts
33 lines (27 loc) · 959 Bytes
/
findKthLargest.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
import { BinaryHeap, ascend } from '../../@std/data-structures';
type findKthLargest = (nums: number[], k: number) => number;
/**
* Accepted
*/
export const findKthLargest: findKthLargest = (nums, k) => {
// Create a min-heap that will store the top k largest elements
const minHeap = new BinaryHeap<number>(ascend);
// Iterate over the array
for (const num of nums) {
// Add the current number to the heap
minHeap.push(num);
// If the heap size exceeds k, remove the smallest element
if (minHeap.length > k) {
minHeap.pop(); // Removes the smallest element, which is at the root of the min-heap
}
}
// The root of the min-heap is the kth largest element
return minHeap.peek() || 0; // Peek returns the smallest element in the heap, which is the kth largest in the array
};
/**
* Accepted
*/
export const findKthLargest2: findKthLargest = (nums, k) => {
nums.sort((a, b) => b - a);
return nums[k - 1];
};