import java.util.*; public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) { ArrayList<Integer> res = new ArrayList<>(); if (input == null || input.length < 1 || input.length < k) { return res; } if (k < 1) { return res; } int n = input.length; PriorityQueue<Integer> pq = new PriorityQueue<>((o1, o2) -> o2 - o1); for (int i = 0; i < n; i++) { if (i < k) { pq.offer(input[i]); } else if (pq.peek() > input[i]) { pq.poll(); pq.offer(input[i]); } } while (!pq.isEmpty()) { res.add(pq.poll()); } return res; } }