Leetcode 1604 Solution

This article provides solution to leetcode question 1604 (least-number-of-unique-integers-after-k-removals)

https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals

Solution

class Solution:
    def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
        cnts = collections.defaultdict(int)
        for v in arr:
            cnts[v] += 1

        q = []
        for v in cnts.values():
            heapq.heappush(q, v)

        while q:
            v = heappop(q)
            if k >= v:
                k -= v
            else:
                return len(q) + 1

        return 0