Problem 445646 · medium · Phase 04 Non-Linear Data Structures

Silence Half the Log

heaps · counting · greedy · max-heap

A log contains codes, a list of integer event codes, with many repeats. You may choose a set of codes and delete every entry with those codes. Return the smallest number of codes you must choose so that at least half of the entries are deleted (for an odd length n, at least (n + 1) // 2 entries). An empty log needs 0 codes.

Examples

Input:  codes = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4]
Output: 2
Explanation: deleting code 3 removes 4 of 10 entries; adding code 1 removes 7 >= 5.

Input:  codes = [7, 7, 7, 7]
Output: 1

Input:  codes = [1, 2, 3, 4, 5, 6]
Output: 3

Constraints

  • 0 <= len(codes) <= 10**5, -10**9 <= codes[i] <= 10**9
  • Target complexity: O(n + d log d) where d is the number of distinct codes.

Goals

  • Reduce a list to its frequency profile
  • Consume the largest counts first with a max-heap
  • Stop as soon as a threshold is met
Starting Python…