leetcode-mid-others-621. Task Scheduler
2024-08-30 01:56:24
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
最新文章
- Vertica删除历史分区数据
- 修改git的远程仓库命令
- WebSocket 介绍(一)
- ubuntu系统修改mysql字符集
- C#删除datable空行
- 如何参与一个 GitHub 开源项目?
- js图形网站
- android ViewPaper高度自适应
- CS程序,服务器端弹出MessageBox.Show()之类的UI操作???禁止
- HDU 2256 Problem of Precision (矩阵快速幂)
- android错误之android.content.res.Resources$NotFoundException:
- SSH中post提交表单action中文乱码问题
- bzoj千题计划217:bzoj2333: [SCOI2011]棘手的操作
- eShopOnContainers 看微服务③:Identity Service
- GitHub下载提速
- [DOM基础]offsetHeight,clientHeight,scrollHeight,innerHeight,outerHeight等属性的解释
- 虚拟机安装CentOS7之后没有ip的问题
- Python开发【笔记】:PEP 8 编码规范
- python之路之迭代器与生成器
- empireCMS 帝国cms功能总结
热门文章
- css练习小总结
- IntelliJ IDEA 2017 提示“Unmapped Spring configuration files found.Please configure Spring facet.”解决办法
- MySQL主备同步延迟
- Django设置 DEBUG=False后静态文件无法加载
- Java函数优雅之道
- squid代理简介
- docker 安装与使用的相关问题
- 状压DP操作
- requests中text,content,json之间的区别
- 动态树(Link-Cut-Tree)简单总结(指针版本)