1338. Reduce Array Size to The Half

    技术2025-10-25  8

    Reduce Array Size to The Half class Solution { public int minSetSize(int[] arr) { int size = arr.length; int resultSize = 0; HashMap<Integer, Integer> hm = new HashMap<>(); for (Integer i : arr) hm.put(i, hm.getOrDefault(i, 0) + 1); PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>( (a, b) -> -a.getValue() + b.getValue() ); for (Map.Entry<Integer, Integer> v : hm.entrySet()) { pq.add(v); } LinkedList<Integer> ll = new LinkedList<>(); LinkedList<Integer> tails = new LinkedList<>(); int c = 0; Map.Entry<Integer, Integer> current; while (c < (size + 1) / 2) { current = pq.poll(); ll.add(current.getKey()); c += current.getValue(); } return ll.size(); } }
    Processed: 0.009, SQL: 9