题目描述:

方法:

class Solution(object):
def maxEqualFreq(self, A):
count = collections.Counter()
freqs = collections.Counter()
ans = 1
for i, x in enumerate(A):
count[x] += 1
c = count[x]
freqs[c] += 1
if c-1 > 0: freqs[c-1] -= 1
if freqs[c-1] == 0:
del freqs[c-1]
L = len(freqs)
#print freqs
if L <= 2:
if L <= 1:
k, = freqs.keys()
fk = freqs[k]
#print '.', i+1, ';', k, freqs[k]
if k == 1 or fk == 1:
ans = i + 1
else:
a, b = freqs.keys()
fa, fb = freqs[a], freqs[b]
# remove a
#print i+1,a,fa,';',b,fb
if fa-1 == 0 and a-1 == b:
ans = i + 1
if fb-1 == 0 and b-1 == a:
ans = i + 1
if a == 1 and fa == 1 or b == 1 and fb == 1:
ans = i + 1
return ans

另:倒推

from collections import Counter
class Solution(object):
def maxEqualFreq(self, nums):
cnt = Counter(nums)
l = len(cnt)
s = n = len(nums)
if n==1:
return 1
for i in range(n-1,0,-1):
if (s-1) % l == 0:
k = (s-1)//l
if all(x==k or x==k+1 for x in cnt.values()):
return i+1
if l!=1 and (s-1) % (l-1) == 0:
k = (s-1)//(l-1)
if all(x==k or x==1 for x in cnt.values()):
return i+1
num = nums[i]
s-=1
cnt[num]-=1
if cnt[num]==0:
del cnt[num]
l-=1
return 1

最新文章

  1. 查询java 类加载的路径
  2. DropDownList 选中change
  3. okhttputils开源库的混淆配置(Eclipse)
  4. Web Service 初步了解
  5. Delphi 中 COM 实现研究手记(一)
  6. Servlet中通过过滤器实现统一的手动编码(解决中文乱码)
  7. 详解k8s原生的集群监控方案(Heapster+InfluxDB+Grafana) - kubernetes
  8. Command &quot;python setup.py egg_info&quot; failed with error code 1 in C:\Users\w5659\AppData\Local\Temp\pip-install-t7uomu4r\xa dmin\
  9. AlwaysUp使用方法
  10. Alfred 3 如何设置默认搜索引擎(以百度搜索为例)
  11. Skipping acquire of configured file &#183;&#183;&#183;doesn&#39;t support architecture &#39;i386&#39; acquire of configured file
  12. bzoj1861
  13. Java实验二
  14. js中的变量作用域问题
  15. SVN集成compare4比较软件
  16. FWT学习笔记
  17. error “Device supports x86, but APK only supports armeabi-v7a”
  18. 如何选择mysql存储引擎
  19. (转)2009-05-25 22:12 Outlook2007选择发送帐号
  20. mysql 操作时间戳

热门文章

  1. 一些笔记jexcel
  2. 【JavaWeb项目】一个众筹网站的开发(六)后台用户权限控制
  3. leetcode-162周赛-1253-重构二进制矩阵
  4. 【JZOJ6433】【luoguP5664】【CSP-S2019】Emiya 家今天的饭
  5. 画PCB时检查点总结
  6. Confluence 6 站点备份和恢复
  7. 数论+乱搞——cf181B
  8. 质数密度+思维——cf1174D
  9. 【Shiro】一、Apache Shiro简介
  10. (转)OpenFire源码学习之十五:插件开发