LeetCode题解(1224):最大相等频率(Python)
发布日期:2021-06-29 20:09:50 浏览次数:2 分类:技术文章

本文共 1669 字,大约阅读时间需要 5 分钟。

题目:(困难)

标签:哈希表、数学

解法 时间复杂度 空间复杂度 执行用时
Ans 1 (Python) 超出时间限制
Ans 2 (Python) O ( N l o g N ) O(NlogN) O(NlogN) O ( N ) O(N) O(N) 160ms (81.02%)
Ans 3 (Python)

解法一:

class Solution:    def maxEqualFreq(self, nums: List[int]) -> int:        size = len(set(nums))        count1 = collections.Counter()        count2 = collections.Counter({
0: size}) ans = 1 for i, n in enumerate(nums): count2[count1[n]] -= 1 count1[n] += 1 count2[count1[n]] += 1 lst1 = [v for k, v in count2.items() if k != 0 and v != 0] lst2 = [k for k, v in count2.items() if k != 0 and v != 0] # print(i, ":", count1, count2, "->", lst1, lst2) # 如果当前所有的数出现次数都是相同的 if len(lst1) == 1: # 如果它们每个数的出现次数不是1 if lst1[0] != 1: # 那么如果它们不只1个数,则无法实现 if lst2[0] != 1: continue # 如果当前所有的数有两种不同的出现次数 if len(lst1) == 2: # 如果出现少的一个次数不是一次,则无法实现 if min(lst1) != 1: continue # 如果当前所有的数有三种不同的出现次数,则注定无法实现 if len(lst1) > 2: continue ans = max(ans, i + 1) return ans

解法二:

class Solution:    def maxEqualFreq(self, nums: List[int]) -> int:        nums += [inf]        count = collections.Counter(nums)        # 从后往前遍历        for i in reversed(range(len(nums))):            count[nums[i]] -= 1            lst = sorted(filter(lambda x: x > 0, count.values()))  # 剔除掉是0的出现频率            if len(lst) == 1 or (lst[0] == 1 and lst[1] == lst[-1]) or (lst[0] == lst[-2] == lst[-1] - 1):                return i

转载地址:https://dataartist.blog.csdn.net/article/details/109850943 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:LeetCode题解(1244):力扣排行榜(Python)
下一篇:LeetCode题解(1213):三个有序数组的交集(Python)

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月10日 21时28分11秒