class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if(k == 0 || arr.length == 0)
return new int[0];
return quickSearch(arr, 0, arr.length - 1, k - 1);
}
private int[] quickSearch(int[] nums, int lo, int hi, int k){
int j = partition(nums, lo, hi);
if(j == k){
return Arrays.copyOf(nums, j + 1);
}
return j > k ? quickSearch(nums, lo, j - 1, k) : quickSearch(nums, j + 1, hi, k);
}
private int partition(int[] nums, int lo, int hi){
int v = nums[lo];
int i = lo, j = hi + 1;
while(true){
while(++i <= hi && nums[i] < v);
while(--j >= lo && nums[j] > v);
if(i >= j)
break;
int t = nums[j];
nums[j] = nums[i];
nums[i] = t;
}
nums[lo] = nums[j];
nums[j] = v;
return j;
}
}
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
int[] vec = new int[k];
if(k == 0)
return vec;
PriorityQueue<Integer> queue = new PriorityQueue<Integer>(k, (a, b)->b - a);
for(int i = 0; i < k; i++)
queue.offer(arr[i]);
for(int i = k; i < arr.length; i++){
if(arr[i] < queue.peek()){
queue.poll();
queue.offer(arr[i]);
}
}
for (int i = 0; i < k; ++i) {
vec[i] = queue.poll();
}
return vec;
}
}