-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy path0215. Kth Largest Element in an Array.js
69 lines (60 loc) · 1.54 KB
/
0215. Kth Largest Element in an Array.js
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
// Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
//
// Example 1:
//
// Input: [3,2,1,5,6,4] and k = 2
// Output: 5
//
// Example 2:
//
// Input: [3,2,3,1,2,4,5,5,6] and k = 4
// Output: 4
//
// Note:
// You may assume k is always valid, 1 ≤ k ≤ array's length.
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
// 1) Sorting
// Time O(n log n)
// Space O(1)
const findKthLargest1 = (nums, k) => {
return nums.sort((a, b) => b - a)[k - 1];
};
// 2)
// Time O(n)
// Space O(1)
//
// https://leetcode.com/problems/kth-largest-element-in-an-array/discuss/60294/Solution-explained
// 3) Quick sort idea
const findKthLargest = (nums, k) => {
const swap = (i, j) => [nums[i], nums[j]] = [nums[j], nums[i]];
const quickSelect = (l, r, k) => {
// Quick sort idea
// put nums that are <= pivot to the left
// put nums that are > pivot to the right
let p = l;
for (let j = l; j < r; j++) {
if (nums[j] <= nums[r]) {
swap(p, j);
p++;
}
}
swap(p, r);
// count the nums that are > pivot
const count = r - p + 1;
if (count > k) {
// pivot is too small, so it must be on the right
return quickSelect(p + 1, r, k);
} else if (count < k) {
// pivot is too big, so it must be on the left
return quickSelect(l, p - 1, k - count);
} else {
return nums[p];
}
};
return quickSelect(0, nums.length - 1, k);
};
export default findKthLargest;