Leetcode 1464 Solution

This article provides solution to leetcode question 1464 (reduce-array-size-to-the-half).

https://leetcode.com/problems/reduce-array-size-to-the-half

Solution

class Solution:
    def minSetSize(self, arr: List[int]) -> int:
        m = collections.defaultdict(int)
        for v in arr:
            m[v] += 1
        
        a = [(v, k) for k, v in m.items()]
        a.sort(reverse=True)

        cnt = 0
        ans = 0
        for v, _ in a:
            cnt += v
            ans += 1
            
            if cnt >= len(arr) // 2:
                break

        return ans