public static ArrayList<Integer> findNSmallestElements(ArrayList<Integer> nums, int n) {
        if (nums == null || n < 0) return null;
        PriorityQueue<Integer> maxheap = new PriorityQueue<Integer>(n, Collections.reverseOrder());
        for (int num : nums) {
            if (maxheap.size() < n) {
                maxheap.offer(num);
            } else {
                if (num < maxheap.peek()) {
                    maxheap.poll();
                    maxheap.offer(num);
                }
            }
        }

        ArrayList<Integer> result = new ArrayList<Integer>();
        while (!maxheap.isEmpty() ) {
            result.add(maxheap.poll());
        }
        return result;
    }