-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkth-largest-element-in-an-array.js
More file actions
114 lines (98 loc) · 2.93 KB
/
kth-largest-element-in-an-array.js
File metadata and controls
114 lines (98 loc) · 2.93 KB
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
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function (nums, k) {
// Step 1: Sort the array in descending order
nums.sort((a, b) => b - a);
// Step 2: Return the Kth largest element
return nums[k - 1];
};
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function (nums, k) {
// Find the smallest and largest values in the array
const minValue = Math.min(...nums); // Minimum value in nums
const maxValue = Math.max(...nums); // Maximum value in nums
// Create a frequency array (count) to store the occurrences of each number
// The size of the array is determined by the range of values: maxValue - minValue + 1
const count = new Array(maxValue - minValue + 1).fill(0);
// Fill the count array
// For each number in nums, increment its respective position in the count array
for (const num of nums) {
count[num - minValue]++; // Map num to count array index using `num - minValue`
}
// Iterate backward over the count array to find the kth largest element
let remaining = k; // Number of elements we need to find
for (let i = count.length - 1; i >= 0; i--) {
remaining -= count[i]; // Subtract the frequency of the current number from remaining
// If remaining becomes 0 or less, we've found the kth largest element
if (remaining <= 0) {
return i + minValue; // Map the index back to the original number using `i + minValue`
}
}
};
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function (nums, k) {
const MinHeap = function () {
this.heap = [];
};
MinHeap.prototype.push = function (val) {
this.heap.push(val);
this.heap.sort((a, b) => a - b);
};
MinHeap.prototype.pop = function () {
return this.heap.shift();
};
MinHeap.prototype.size = function () {
return this.heap.length;
};
let heap = new MinHeap();
for (let num of nums) {
heap.push(num);
if (heap.size() > k) {
heap.pop();
}
}
return heap.heap[0];
};
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function (nums, k) {
const partition = (left, right) => {
const pivot = nums[right];
let partitionIndex = left;
for (let i = left; i < right; i++) {
if (nums[i] >= pivot) {
// Descending order comparison
[nums[i], nums[partitionIndex]] = [nums[partitionIndex], nums[i]];
partitionIndex++;
}
}
[nums[partitionIndex], nums[right]] = [nums[right], nums[partitionIndex]];
return partitionIndex;
};
let left = 0,
right = nums.length - 1;
while (true) {
const pivotIndex = partition(left, right);
if (pivotIndex === k - 1) {
return nums[pivotIndex];
} else if (pivotIndex < k - 1) {
left = pivotIndex + 1;
} else {
right = pivotIndex - 1;
}
}
};