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