Least Number of Unique Integers after K Removals

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

 

# https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/discuss/686343/Python-oror-3-Line-oror-Shortest-Simplest
# remove "elements which have least count"
 
    def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
        counter = collections.Counter(arr)
        # print(counter). 计数器是有序的
        # Counter({3: 3, 1: 2, 4: 1, 2: 1})

        # 按元素出现次数counter[x]对数组arr进行排序
        s = sorted(arr,key = lambda x:(counter[x],x))
        # print(s)
        return len(set(s[k:]))
View Code

 

posted @ 2022-11-09 08:01  7aughing  阅读(8)  评论(0编辑  收藏  举报