当前位置:网站首页>LeetCode·621.任务调度器·构造法
LeetCode·621.任务调度器·构造法
2022-07-29 14:35:00 【小迅想变强】
链接:https://leetcode.cn/problems/task-scheduler/solution/by-xun-ge-v-akns/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
解题思路
按照此图进行构造即可
题目要求相同的任务中间需要休息n个单位,我们想要在最短的时间内完成所有任务,那么就需要在相同任务的n个单位休息中添加其他任务,来消耗休息时间,我们取相同任务最多为构造最长边,因为相同任务需要休息n个单位,那么休息时间最长的就是相同任务最多的。
这里主要优化一下过程,我们在计算任务数时,就已经对最多最小的任务进行了计算,那么我们只需要设置两个变量,统计一下最多任务数和最多任务数的个数即可,不需要额外再对这些变量进行统计
代码
#define MAX(a , b) ((a) > (b) ? (a) : (b))
int leastInterval(char* tasks, int tasksSize, int n){
int str[26];
memset(str, 0, sizeof(int) * 26);//初始化
int max = -1;
int cunst = 0;
for(int i = 0; i < tasksSize; i++)//遍历任务数组
{
str[tasks[i] - 'A']++;
if(max < str[tasks[i] - 'A'])//统计最大值
{
max = str[tasks[i] - 'A'];
cunst = 1;
}
else if(max == str[tasks[i] - 'A'])//最大值相同数+1
{
cunst++;
}
}
return MAX(((max-1)*(n+1)+cunst) , tasksSize);
}
作者:xun-ge-v
链接:https://leetcode.cn/problems/task-scheduler/solution/by-xun-ge-v-akns/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间空间复杂度
边栏推荐
- WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1
- Pinia状态持久化
- 兆骑科创海外高层次人才引进平台,企业项目对接,赛事活动路演
- Introduction to several methods of making custom welcome interface on Weiluntong touch screen
- AOP implementation enterprise API access interface monitoring (via Google Guava cache data)
- <老式喜剧>
- 超好用的PC端录屏软件推荐
- How to get local json
- 求教一下 现在最新版的flinkcdc能获取到oracle的ddl变更信息吗?
- arcgis中编码方式改变引起的shp文件乱码、字符截断问题处理
猜你喜欢
随机推荐
AOP实现企业级API访问接口监控(通过Google Guava缓存数据)
Based on domestic, link global | schneider electric "industrial SI alliance partners hand in hand" to the industry in the future
【 LeetCode 】 121. The best time to buy stocks
rosbag数据画图MATLAB
全面质量管理理论
ArcGIS Pro与ArcGis区别
arcpy脚本制作arcgis工具箱注意事项
WOLFLAB一方老师带你解读虚拟云网络《VMware NSX-T卷2》-1
面对互联网的裁员潮,我们该何去何从?
Map遍历 key-value 的4种方法
测试时间的评估:开发时间的1/3~1/2
深陷盈利困境,“寒冬”中也要二次递表,北森上市迫切
Programmers are a group with a high incidence of occupational diseases. Don’t be naive to think that it’s just as simple as being bald.
Introduction to several methods of making custom welcome interface on Weiluntong touch screen
函数柯里化
【IIC通信】Chap.2 (I2C)IIC协议的特点;为什么IIC需要开漏输出、上拉电阻?
这 6 款在线 PDF 转换工具,得试
这 6 款在线 PDF 转换工具,得试试
部门例会上做测试分享,不知道分享什么内容?
基于SSM实现在线聊天系统