forked from jyxia/LeetCode-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path215-KthLargestElementInArray.js
52 lines (50 loc) · 1.26 KB
/
215-KthLargestElementInArray.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
/**
* Key:
* Quick select algorithm, choose last element of the array as the pivot.
* Move all smaller elements than the pivot to the left of the pivot,
* move all bigger elements than the pivot to the right of the pivot,
* repeat this process until the pivot is K
* O(N)? worst O(N^2)
*
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var findKthLargest = function(nums, k) {
k = nums.length - k;
var start = 0;
var end = nums.length - 1;
while (start <= end) {
var pivot = partition(nums, start, end);
if (k === pivot) {
return nums[pivot];
}
else if (k < pivot) {
end = pivot - 1;
} else {
start = pivot + 1;
}
}
};
var partition = function(nums, start, end) {
var left = start;
var right = end;
var pivot = end;
while (true) {
while (left < right && nums[left] < nums[pivot]) {
left++;
}
while (left < right && nums[right] >= nums[pivot]) {
right--;
}
if (left === right) break;
swap(nums, left, right);
}
swap(nums, right, end);
return right;
};
var swap = function(nums, i, j) {
var tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
};