mycode   53.01%

这个题在纸上画一画就知道啦,只要出现次数最多的字母能够满足要求,其他更少的字母穿插在其中,间隔就更满足<n啦,当然,最后不要忘记加上尾巴哦,尾巴和出现次数最多的字母的种类有关哦!

class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
from collections import Counter
count = Counter(tasks)
total = 1
maxcount = count.most_common()[0][1]
print(maxcount)
for num,c in count.most_common()[1:]:
if c == maxcount:
total += 1
return max(len(tasks),(maxcount-1)*(n+1)+total)

参考:

class Solution(object):
def leastInterval(self, tasks, n):
"""
:type tasks: List[str]
:type n: int
:rtype: int
"""
lenT = len(tasks)
res = rem = max_cnt = 0
dic = collections.defaultdict(int) for ch in tasks:
dic[ch]+=1
for val in dic.values():
if val>max_cnt:
max_cnt = val
rem = 0
elif val==max_cnt:
rem+=1 #与max_cnt出现频率相同的其他元素个数
tmp = (max_cnt-1)*(n+1)+1 + rem # n很长的情况
res = max(lenT, tmp)
return res

最新文章

  1. Vertica删除历史分区数据
  2. 修改git的远程仓库命令
  3. WebSocket 介绍(一)
  4. ubuntu系统修改mysql字符集
  5. C#删除datable空行
  6. 如何参与一个 GitHub 开源项目?
  7. js图形网站
  8. android ViewPaper高度自适应
  9. CS程序,服务器端弹出MessageBox.Show()之类的UI操作???禁止
  10. HDU 2256 Problem of Precision (矩阵快速幂)
  11. android错误之android.content.res.Resources$NotFoundException:
  12. SSH中post提交表单action中文乱码问题
  13. bzoj千题计划217:bzoj2333: [SCOI2011]棘手的操作
  14. eShopOnContainers 看微服务③:Identity Service
  15. GitHub下载提速
  16. [DOM基础]offsetHeight,clientHeight,scrollHeight,innerHeight,outerHeight等属性的解释
  17. 虚拟机安装CentOS7之后没有ip的问题
  18. Python开发【笔记】:PEP 8 编码规范
  19. python之路之迭代器与生成器
  20. empireCMS 帝国cms功能总结

热门文章

  1. css练习小总结
  2. IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法
  3. MySQL主备同步延迟
  4. Django设置 DEBUG=False后静态文件无法加载
  5. Java函数优雅之道
  6. squid代理简介
  7. docker 安装与使用的相关问题
  8. 状压DP操作
  9. requests中text,content,json之间的区别
  10. 动态树(Link-Cut-Tree)简单总结(指针版本)