专栏首页SnailTyanLeetcode 1481. Least Number of Unique Integers after K Removals

Leetcode 1481. Least Number of Unique Integers after K Removals

文章作者:Tyan 博客:noahsnail.com | CSDN | 简书

1. Description

2. Solution

  • Version 1
class Solution:
    def findLeastNumOfUniqueInts(self, arr, k):
        stat = {}
        for num in arr:
            stat[num] = stat.get(num, 0) + 1
        result = sorted(stat.items(), key=lambda item: item[1])
        while k > 0:
            k = k - result[0][1]
            if k >= 0:
                result.pop(0)
        return len(result)
  • Version 2
class Solution:
    def findLeastNumOfUniqueInts(self, arr, k):
        stat = {}
        for num in arr:
            stat[num] = stat.get(num, 0) + 1
        result = sorted(stat.items(), key=lambda item: item[1])
        index = 0
        while k > 0:
            k = k - result[index][1]
            if k >= 0:
                index += 1
        return len(result) - index
  • Version 3
class Solution:
    def findLeastNumOfUniqueInts(self, arr, k):
        stat = {}
        for num in arr:
            stat[num] = stat.get(num, 0) + 1
        result = sorted(arr, key=lambda num: (stat[num], num))
        return len(set(result[k:]))

Reference

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • CodeForces 732B Cormen — The Best Friend Of a Man

    B. Cormen — The Best Friend Of a Man time limit per test 1 second memory lim...

    ShenduCC
  • Codeforces 1291 Round #616 (Div. 2) C. Mind Control(超级详细)

    You and your n−1 friends have found an array of integers a1,a2,…,an. You have de...

    风骨散人Chiam
  • CodeForces 639 A

    Bear and Displayed Friends time limit per test2 seconds memory limit per tes...

    ShenduCC
  • 【CodeForces 489A】SwapSort

    In this problem your goal is to sort an array consisting of n integers in at mos...

    饶文津
  • Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes inp...

    Angel_Kitty
  • LWC 69: 773. Sliding Puzzle

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.n...

    用户1147447
  • 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D

    饶文津
  • Ignatius and the Princess IV hdu 1029

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Oth...

    用户2965768
  • C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    在线提交: https://leetcode.com/problems/top-k-frequent-elements/

    Enjoy233

扫码关注云+社区

领取腾讯云代金券