-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kth Largest Element in an Array.java
38 lines (37 loc) · 1.12 KB
/
Kth Largest Element in an Array.java
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
class Solution {
public int findKthLargest(int[] nums, int k) {
return quickSelect(nums, 0, nums.length - 1, k);
}
private int quickSelect(int[] nums, int start, int end, int k) {
int index = partition(nums, start, end, k);
int right = end - index + 1;
if (right == k) {
return nums[index];
}
if (right > k) {
return quickSelect(nums, index + 1, end, k);
}
else {
return quickSelect(nums, start, index - 1, k - right);
}
}
private int partition(int[] nums, int start, int end, int k) {
int pivot = nums[end];
int index = end - 1;
while (start <= index) {
if (nums[start] > pivot) {
while (index > start && nums[index] > pivot) {
index --;
}
int tmp = nums[start];
nums[start] = nums[index];
nums[index] = tmp;
index --;
}
start ++;
}
nums[end] = nums[index + 1];
nums[index + 1] = pivot;
return index + 1;
}
}