-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth largest element in an array.java
62 lines (50 loc) · 1.44 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://leetcode.com/problems/kth-largest-element-in-an-array/
class Solution {
public int findKthLargest(int[] nums, int k) {
k = nums.length - k + 1;
int waste = solve(nums, k, 0, nums.length-1);
return nums[k-1];
}
int solve(int []nums, int k, int lb, int ub)
{
int idx = Partition(nums, lb, ub);
if(idx == k-1)
return nums[idx];
if(k - 1 < idx)
solve(nums, k, lb, idx - 1);
else
solve(nums, k, idx + 1, ub);
return 0;
}
void swap(int []a, int i, int j)
{
int t = a[i];
a[i] = a[j];
a[j] = t;
}
void randompivot(int nums[], int lb,int ub)
{
int range = ub - lb + 1;
int pivot = ((int)(Math.random() * range) + lb);
swap(nums, pivot, lb);
}
int Partition(int nums[], int lb, int ub)
{
if(lb == ub) return lb;
randompivot(nums, lb, ub);
int pivot = lb;
int start = lb + 1, end = ub;
while(start <= end)
{
while(start <= end && nums[start] <= nums[pivot])
start++;
while(end >= start && nums[end] > nums[pivot])
end--;
if(start > end)
swap(nums, pivot, end);
else
swap(nums, start, end);
}
return end;
}
}