题目如下:

Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences.

If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0).

Example 1:

Input: nums = [2,2,1,1,5,3,3,5]
Output: 7
Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4]=5, we will get [2,2,1,1,3,3],
so that each number will appear exactly twice.

Example 2:

Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output: 13

Example 3:

Input: nums = [1,1,1,2,2,2]
Output: 5

Example 4:

Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6]
Output: 8

Constraints:

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

解题思路:从头开始遍历nums,很容易可以求出0~i区间内每个数字出现的次数,并用dic_count存储,key为数字的值,而value为数字出现的次数。同时再用dic记录dic_count中每个次数出现的次数。例如 [1,1,1,2,2,2,3,3,3,4,4,4,5] 区间,dic_count = {1: 3, 2: 3, 3: 3, 4: 3, 5: 1},而dic = {1: 1, 3: 4} ,这里1:1表示在dic_count中出现1次的数字有1个,出现3次的数字有四个。很显然,要判断0~i区间是否删除一个元素后可以使得所有元素出现的次数一样,需要满足以下条件:

1. 如果len(dic)为1,只要所有的元素都只出现一次,那么这个区间就是满足题目要求的;

2. 如果len(dic)为2,需要满足有且仅有一个元素出现一次,表示可以删除掉这个元素。或者是有且仅有一个元素出现的次数是其他元素出现的次数+1,表示删除掉这个元素后就使得其和其他元素出现次数一致。

代码如下:

class Solution(object):
def maxEqualFreq(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
dic_count = {}
dic = {}
for i in range(len(nums)):
if nums[i] in dic_count:
dic_count[nums[i]] += 1
count = dic_count[nums[i]]
dic[count] = dic.setdefault(count, 0) + 1
if count > 1 and count - 1 in dic:
dic[count - 1] -= 1
if dic[count - 1] == 0:
del dic[count - 1]
else:
dic_count[nums[i]] = 1
count = dic_count[nums[i]]
dic[count] = dic.setdefault(count, 0) + 1 if len(dic) == 2:
k1 = k2 = None
for key in dic.iterkeys():
if k1 == None:
k1 = key
continue
k2 = key
#if abs(k1-k2) == 1 and (dic[k1] == 1 or dic[k2] == 1):res = (i+1)
if (k1 - k2 == 1 and dic[k1] == 1) or (k2 - k1 == 1 and dic[k2] == 1):res = i+1
elif (k1 == dic[k1] == 1) or (k2 == 1 and dic[k2] == 1):res = i + 1
elif len(dic) == 1:
# key is 1 : ex, input is [1,0]
if 1 in dic and dic[1] > 0:res = i+1
else:
for val in dic.itervalues():
if val == 1:res = i+1
return res

【leetcode】1224. Maximum Equal Frequency的更多相关文章

  1. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  2. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  3. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  6. 【leetcode】1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold

    题目如下: Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square ...

  7. 【leetcode】1255. Maximum Score Words Formed by Letters

    题目如下: Given a list of words, list of  single letters (might be repeating) and score of every charact ...

  8. 【leetcode】1208. Get Equal Substrings Within Budget

    题目如下: You are given two strings s and t of the same length. You want to change s to t. Changing the  ...

  9. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

随机推荐

  1. Docker安装报错

    这是由于overlay2不支持造成的,所以我们要关闭它. 解决办法: vim /etc/sysconfig/docker    (修改文件) DNS='--selinux-enabled --log- ...

  2. centos 7 ip a 或ifconfig 报command not found

    CentOS 7 下 ifconfig command not found 或 ip command not found 解决办法 首先查看:/sbin/ifconfig   /sbin/ip 是否存 ...

  3. Pycharm最新激活码汇总,pycharm2019激活码

    Pycharm激活码汇总 激活过程如下: 1.双击运行桌面上的Pycharm图标,进入下图界面,选择Do not import settings,之后选择OK,进入下一步. 2.拖动到底部,选择Acc ...

  4. Python学习之表的数据类型

    数据类型 数值类型 类型 大小 范围(有符号) 范围(无符号)unsigned约束 用途 TINYINT 1 字节 (-128,127) (0,255) 小整数值 SMALLINT 2 字节 (-32 ...

  5. Python学习之进程

    8.2 进程 8.2.1 进程的创建 开启多进程scoketserver:server.client 进程的开启:python中的多线程,一定是有一个主进程,由主进程创建几个子进程, Linux与Wi ...

  6. cmd命令简单别木马的蛛丝马迹

    一些基本的Windows命令往往可以识别木马的蛛丝马迹,而且在保护网络安全上起到很大的作用. 检测网络连接 如果你怀疑自己的计算机上被别人安装了木马,或者是中了病毒,但是手里没有完善的工具来检测是不是 ...

  7. CDH开启ldap

    参考: 官网ldap: https://www.cloudera.com/documentation/enterprise/6/6.2/topics/cm_sg_ldap_grp_mappings.h ...

  8. Axios 的基本使用

    Axios 是一个基于 promise 的HTTP 库, 可以用在浏览器和 node.js 中. 1. 从浏览器创建 XMLHttpRequests 2. 从node.js 创建 http 请求 3. ...

  9. oracle ojdbc 版本须对应,否则日期字段查询结果与实际值可能不一致

    1. 数据库版本:select * from v$version; 2. 版本对应:

  10. Java作业 题目:16版.真实员工数统计

    题目:16版.真实员工数统计 该资源支持按部自动给分,评分规则如下: sjkdfhslkfdhdsiog函数定义测试 sjkdfhslkfdhdsiog函数定义测试 sjkdfhslkfdhdsiog ...