[抄题]:

Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

However, there is a non-negative cooling interval n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle.

You need to return the least number of intervals the CPU will take to finish all the given tasks.

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

知道不同字母之间互相制约,但是不知道抓主要矛盾:把字母频次由高到低统计

[一句话思路]:

字母种类少时套公式: 间隔数*(间隔长度+1自身)+ 补齐自身

 (max - 1) * (n + 1) + maxNum 

,种类多时直接用数组长

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

多个字母都会影响时,选择频率最高的主要影响

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[算法思想:递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public int leastInterval(char[] tasks, int n) {
//cc //ini: char[26]
int[] letters = new int[26];
int i = 25; //store in char[26], sort, count frequency
for (char c : tasks) {
letters[c - 'A']++;
}
Arrays.sort(letters);
while (i >= 0 && letters[i] == letters[25]) i--; //return res
return Math.max(tasks.length, (n + 1) * (letters[25] - 1) + 25 - i);
}
}

621. Task Scheduler CPU任务间隔分配器的更多相关文章

  1. LeetCode 621. Task Scheduler

    原题链接在这里:https://leetcode.com/problems/task-scheduler/description/ 题目: Given a char array representin ...

  2. 【LeetCode】621. Task Scheduler 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 公式法 日期 题目地址:https://leetco ...

  3. [LeetCode] 621. Task Scheduler 任务调度

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  4. [leetcode]621. Task Scheduler任务调度

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  5. [LeetCode]621. Task Scheduler 任务安排 题解

    题目描述 给定一个char数组,代表CPU需要做的任务,包含A-Z,不用考虑顺序,每个任务能在1个单位完成.但是有规定一个非负整数n代表两个相同任务之间需要至少n个时间单位.球最少数量的时间单位完成所 ...

  6. 621. Task Scheduler

    https://www.cnblogs.com/grandyang/p/7098764.html 将个数出现最多的那个字符作为分隔的标准,一定是最小的.所以这个时候只需要计算还需要添加多少个idel就 ...

  7. [leetcode] 621. Task Scheduler(medium)

    原题 思路: 按频率最大的字母来分块,频率最大的字母个数-1为分成的块数,每一块个数为n+1 比如AAABBCE,n=2, 则分为A-A- +A AAABBBCCEE,n=2,则分为AB-AB- +A ...

  8. 使用C#创建计划任务(How to create a Task Scheduler use C# )

    本文主要讲解了如何使用C#来创建windows计划任务. 需求:在不定时间段运行多个后台程序(winfrom,wpf,console,等等)用于更新数据.  问题:为什么要使用计划任务,而不直接在程序 ...

  9. Spring的任务调度@Scheduled注解——task:scheduler和task:executor的解析

    原文地址: https://blog.csdn.net/yx0628/article/details/80873774 一个简单的Spring定时任务的 demo,全部代码见下载地址:https:// ...

随机推荐

  1. Android Activity活动状态及生存周期

    1.活动状态 每个活动在其生命周期中最多可能会有4中状态. (1)运行状态 当一个活动位于返回栈的栈顶时,此时活动就处于运行状态.系统不会回收处于运行状态的活动. (2)暂停状态 当一个活动不再处于栈 ...

  2. python 多线程要点

    要点整理 多线程 #coding=utf-8 import threading from time import ctime,sleep def music(func): for i in range ...

  3. sprinboot+redis

    (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boot</groupId> < ...

  4. HDU 1166 敌兵布阵 (线段树模版题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. Redis: temple

    ylbtech-Redis:  1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部   1 ...

  6. org.hibernate.Session常用方法的作用总结

    clear(): 清理session缓存.把所有加载出来的持久化实例都从session缓存中清除,同时,也会挂掉当前session中正在排队的save,update,delete操作. evict: ...

  7. 文档主题生成模型(LDA)

    一.问题描述 1.1文本建模相关 统计文本建模的目的其实很简单:就是估算一组参数,这组参数使得整个语料库出现的概率最大.这是很简单的极大似然的思想了,就是认为观测到的样本的概率是最大的.建模的目标也是 ...

  8. jQuery笔记——插件

    验证插件 验证插件(validate.js),是一款验证常规表单数据合法性的插件.使用它,极大的解 放了在表单上繁杂的验证过程,并且错误提示显示的完善也增加了用户体验 插件都可以在JQueryUI 的 ...

  9. Intellij解决版本冲突时,merge界面各区块颜色区分

    灰色:删除 蓝色:改变 绿色:新增 红色:冲突

  10. 【BZOJ】3524 [POI2014] Couriers(主席树)

    题目 传送门:QWQ 传送到洛谷QWQ 分析 把求区间第k大的改一改就ok了. 代码 #include <bits/stdc++.h> using namespace std; ; ], ...