-
Notifications
You must be signed in to change notification settings - Fork 81
/
KthLargestElementInAnArray215.java
183 lines (158 loc) · 4.27 KB
/
KthLargestElementInAnArray215.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* 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.
*
* For example,
* Given [3,2,1,5,6,4] and k = 2, return 5.
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ array's length.
*/
public class KthLargestElementInAnArray215 {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
/**
* https://discuss.leetcode.com/topic/14597/solution-explained
*/
public int findKthLargest2(int[] nums, int k) {
final PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int val : nums) {
pq.offer(val);
if(pq.size() > k) {
pq.poll();
}
}
return pq.peek();
}
/**
* https://discuss.leetcode.com/topic/14597/solution-explained
*/
public int findKthLargest3(int[] nums, int k) {
k = nums.length - k;
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
final int j = partition(nums, lo, hi);
if(j < k) {
lo = j + 1;
} else if (j > k) {
hi = j - 1;
} else {
break;
}
}
return nums[k];
}
private int partition(int[] a, int lo, int hi) {
int i = lo;
int j = hi + 1;
while(true) {
while(i < hi && less(a[++i], a[lo]));
while(j > lo && less(a[lo], a[--j]));
if(i >= j) {
break;
}
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
private void exch(int[] a, int i, int j) {
final int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
private boolean less(int v, int w) {
return v < w;
}
/**
* https://discuss.leetcode.com/topic/14597/solution-explained
*/
public int findKthLargest4(int[] nums, int k) {
shuffle(nums);
k = nums.length - k;
int lo = 0;
int hi = nums.length - 1;
while (lo < hi) {
final int j = partition(nums, lo, hi);
if(j < k) {
lo = j + 1;
} else if (j > k) {
hi = j - 1;
} else {
break;
}
}
return nums[k];
}
private void shuffle(int a[]) {
final Random random = new Random();
for(int ind = 1; ind < a.length; ind++) {
final int r = random.nextInt(ind + 1);
exch(a, ind, r);
}
}
public int findKthLargest5(int[] nums, int k) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int n: nums) {
min = Math.min(min, n);
max = Math.max(max, n);
}
if (min == max) return min;
while (min < max) {
int mid = min + (max - min) / 2;
int c = count(nums, mid);
if (c >= k) {
min = mid + 1;
} else {
max = mid;
}
}
return min;
}
private int count(int[] nums, int mid) {
int res = 0;
for (int n: nums) {
if (n > mid) res++;
}
return res;
}
public int findKthLargest6(int[] nums, int k) {
int len = nums.length;
int lo = 0;
int hi = len - 1;
while (lo < hi) {
int p = partition2(nums, lo, hi);
if (p == (len - k)) return nums[p];
if (p > (len - k)) {
hi = p - 1;
} else {
lo = p + 1;
}
}
return nums[lo];
}
private int partition2(int[] nums, int lo, int hi) {
if (lo == hi) return lo;
int slow = lo+1;
int fast = lo+1;
while (fast <= hi) {
if (nums[fast] < nums[lo]) {
swap(nums, slow, fast);
slow++;
}
fast++;
}
swap(nums, lo, slow-1);
return slow-1;
}
private void swap(int[] nums, int i, int j) {
if (i == j) return ;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}