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:**
~~~
Input: tasks = ["A","A","A","B","B","B"], n = 2
Output: 8
Explanation: A -> B -> idle -> A -> B -> idle -> A -> B.
~~~
**Note:**
1. The number of tasks is in the range [1, 10000].
2. The integer n is in the range [0, 100].
![](https://img.kancloud.cn/26/17/26170998f48aef1f927a09620cc07024_1087x486.png)
```
/**
* @param {character[]} tasks
* @param {number} n
* @return {number}
*/
var leastInterval = function(tasks, n) {
let q = ''; //队列的执行结果
let Q = {}; //对归类进行存储
tasks.forEach(item =>{
if(Q[item]){
Q[item]++;
}else{
Q[item] = 1;
}
})
while(1){
let keys = Object.keys(Q);
if(!keys[0]){
break;
}
let tmp = [];
for(let i=0;i<=n;i++){
let max = 0;
let key;
let pos;
keys.forEach((item,idx) =>{
if(Q[item] > max){
max = Q[item];
key = item;
pos = idx;
}
})
if(key){
tmp.push(key);
keys.splice(pos,1);
Q[key]--;
if(Q[key]<1){
delete Q[key];
}
}else{
break;
}
}
q += tmp.join('').padEnd(n+1,'-')
}
//最后不要出现冷却时间
q = q.replace(/-+$/g,'')
return q.length;
}
```
- 说明
- 排序
- java实现
- 二分归并排序
- 快速排序
- js实现
- 冒泡排序 (Bubble Sort)
- 快速排序-js
- ES6-rest参数
- 比较排序
- 堆排序
- 插入排序
- 归并排序
- 选择排序
- shellSort
- 排序基础知识
- 其他
- Rabbit
- chickenProblem
- 汉诺塔
- 取出两个字符串中开头相同的部分
- 两个七进制输入,输出七进制
- 二维数组(矩阵)对角线输出
- 根据概率取值
- 最小编辑距离算法
- 列划分算法
- 数组去重
- 数组去重1
- 在一天里,钟表的时针和分针会重合多少次?
- 找第二大问题
- Raft算法
- B-tree
- Leetcode JAVA实现
- leetcode1. 2 Sum
- leetcode2. Add Two Numbers
- leetcode7. Reverse Integer
- leetcode8. String to Integer (atoi)
- leetcode12. Integer to Roman
- leetcode13. Roman to Integer
- leetcode15. 3Sum
- leetcode18. 4Sum
- leetcode20. Valid Parentheses
- leetcode26. Remove Duplicates from Sorted Array
- leetcode27. Remove Element
- leetcode58. Length of Last Word
- leetcode66. Plus One
- leetcode67. Add Binary
- leetcode80. Remove Duplicates from Sorted Array II
- leetcode 88. Merge Sorted Array
- leetcode101. Symmetric Tree
- leetcode104. Maximum Depth of Binary Tree
- leetcode110. Balanced Binary Tree
- leetcode118. Pascal's Triangle
- leetcode119. Pascal's Triangle II
- leetcode121. Best Time to Buy and Sell Stock
- leetcode125. Valid Palindrome
- leetcode136. Single Number
- leetcode144. Binary Tree Preorder Traversal
- leetcode145. Binary Tree Postorder Traversal
- leetcode155. Min Stack
- leetcode169. Majority Element
- leetcode171. Excel Sheet Column Number
- leetcode172. Factorial Trailing Zeroes
- leetcode189. Rotate Array
- leetcode191. Number of 1 Bits
- leetcode202. Happy Number
- leetcode203. Remove Linked List Elements
- leetcode204. Count Primes
- leetcode225. Implement Stack using Queues
- leetcode226. Invert Binary Tree
- leetcode231. Power of Two
- leetcode232. Implement Queue using Stacks
- leetcode234. Palindrome Linked List
- leetcode237. Delete Node in a Linked List
- leetcode242. Valid Anagram
- leetcode258. Add Digits
- leetcode263. Ugly Number
- leetcode283. Move Zeroes
- leetcode292. Nim Game
- leetcode344. Reverse String
- leetcode371. Sum of Two Integers
- leetcode804. Unique Morse Code Words
- Leetcode JS 实现
- Js-leetcode 1. Two Sum
- Js-leetcode 2. Add Two Numbers
- Js-leetcode 10. Regular Expression Matching
- Js-leetcode 14. Longest Common Prefix
- Js-leetcode 17.Letter Combinations of a Phone Number
- Js-leetcode 20. Valid Parentheses
- Js-leetcode 27. Remove Element
- Js-leetcode 30. Substring with Concatenation of All Words
- Js-leetcode 35. Search Insert Position
- Js-leetcode 41. First Missing Positive
- Js-leetcode 48. Rotate Image
- Js-leetcode 53. Maximum Subarray
- Js-leetcode 54. Spiral Matrix
- Js-leetcode 73. Set Matrix Zeroes
- Js-leetcode 75. Sort Colors
- Js-leetcode 85. Maximal Rectangle
- Js-leetcode 89. Gray Code
- Js-leetcode 93.Restore IP Addresses
- Js-leetcode 98. Validate Binary Search Tree
- Js-leetcode 104. Maximum Depth of Binary Tree
- Js-leetcode 118. Pascal's Triangle
- Js-leetcode 121. Best Time to Buy and Sell Stock
- Js-leetcode 136. Single Number
- Js-leetcode 164. Maximum Gap
- Js-leetcode 189. Rotate Array
- Js-leetcode 215. Kth Largest Element in an Array
- Js-leetcode 217. Contains Duplicate
- Js-leetcode 258. Add Digits
- Js-leetcode 263.Ugly Number
- Js-leetcode 283. Move Zeroes
- Js-leetcode 326. Power of Three
- Js-leetcode 434. Number of Segments in a String
- Js-leetcode 459. Repeated Substring Pattern
- Js-leetcode 507. Perfect Number
- Js-leetcode 537. Complex Number Multiplication
- Js-leetcode 541. Reverse String II
- Js-leetcode 551. Student Attendance Record I
- Js-leetcode 557. Reverse Words in a String III
- Js-leetcode 605. Can Place Flowers
- Js-leetcode 606. Construct String from Binary Tree
- Js-leetcode 621. Task Scheduler
- Js-leetcode 622. Design Circular Queue
- Js-leetcode 633. Sum of Square Numbers
- Js-leetcode 657. Robot Return to Origin
- Js-leetcode 682. Baseball Game
- Js-leetcode 686. Repeated String Match
- Js-leetcode 696. Count Binary Substrings
- Js-leetcode 709.To Lower Case.md
- Js-leetcode 771 Jewels and Stones
- Js-leetcode 804.Unique Morse Code Words
- Js-leetcode 867. Transpose Matrix
- Js-leetcode 905. Sort Array By Parity
- Js-leetcode 914. X of a Kind in a Deck of Cards
- Js-leetcode 922. Sort Array By Parity II
- Js-leetcode 1221. Split a String in Balanced Strings
- Js-leetcode 3. Longest substring without repeating characters
- document
- 刘宇波老师--基础知识
- 算法与数据结构
- 数据机构与算法介绍
- 二叉树
- 堆
- 链表
- 队列
- 并查集
- 最小生成树
- 图论
- 最短路径
- 总结
- 玩转数据结构
- 介绍
- 数组
- 栈和队列
- 玩转链表
- 链表与递归
- 二分搜索树
- 集合与映射
- 第8章 优先队列和堆
- 第9章 线段树
- 第10章 Trie
- 玩转算法面试 leetcode题库分门别类详细解析
- 第一章 介绍
- 第二章 时间复杂度和空间复杂度
- 第三章
- 深度实战玩转算法
- JS数据结构与算法
- 第一章.介绍
- 第二章.字符串
- 第三章.数组
- 第四章
- 第五章.排序
- 第六章.递归
- 第七章.栈
- 第八章.队列
- 第九章.链表
- 第十章.矩阵
- 第十一章.二叉树
- 第十二章.堆
- 极客时间
- 第01课丨数据结构与算法总览
- 第02课丨训练准备和复杂度分析
- 第03课丨数组、链表、跳表
- 第04课丨栈、队列、优先队列、双端队列
- 算法基础
- 哈希表(散列表)
- 异或运算
- leetcode
- KD树(K-Dimension Tree)
- Rtree
- B站-左神
- 一周刷爆LeetCode,算法大神左神(左程云)耗时100天打造算法与数据结构基础到高级全家桶教程,直击BTAJ等一线大厂必问算法面试题真题详解
- 简单的排序算法
- 认识Nlog(N)的算法
- 第三节:桶排序及排序算法总结
- 左程云算法课程
- 1 认识复杂度、对数器、二分法与异或运算
- 2 链表结构、栈、队列、递归行为、哈希表和有序表
- 10道BAT必问算法面试题
- 超级水王问题
- 78节
- 04链表