当前位置:网站首页>leetcode621. task scheduler
leetcode621. task scheduler
2022-07-02 13:19:00 【2021dragon】

LeetCode Series articles
List of articles
One 、 Title Description
Here's an array of characters for you t a s k s tasks tasks It means CPU List of tasks to be performed . Each letter represents a different kind of task . Tasks can be performed in any order , And every task can be in 1 In unit time . At any unit time ,CPU Can complete a task , Or on standby .
However , There must be an integer between two tasks of the same kind n n n The cooling time of , So at least there's continuity n n n In units of time CPU On different missions , Or on standby .
You need to calculate the minimum time required to complete all tasks .
Two 、 Example
Input : tasks = [‘A’, ‘A’, ‘A’, ‘B’, ‘B’, ‘B’], n = 2
Output : 8
explain : A -> B -> ( On standby ) -> A -> B -> ( On standby ) -> A -> B
In this example , The interval length between two tasks of the same type must be n = 2 The cooling time of , It takes only one unit time to perform a task , So in the middle ( On standby ) state .
Be careful : Tasks are expressed in capital English letters .
3、 ... and 、 Main idea
Calculate the minimum time required to complete all tasks according to the task list , First, we need to find the task that needs to be executed the most times in the task list , Then take this task as the timeline for analysis .
Suppose that the task that needs to be executed the most times in the given task list A, Then we can use a matrix to show the execution of tasks A The timing of the . because CPU There must be at least a length of n n n The cooling time of , In order to minimize the cooling time , The width of the matrix should be set to n + 1 n+1 n+1, And the height of the matrix is the task A The number of executions required .

If there are only tasks in the task list A, In addition to performing tasks in this matrix A Out of your position , Other positions correspond to “ Standby status ”, And when the last task is finished A when , All tasks have been completed , So what's left in the last row of the matrix n − 1 n-1 n−1 At this time, the position is not counted into the time required to complete the task ( In the picture below, we use ╳ Express ).
The time required to complete all tasks at this time is : ( M a x E x e c − 1 ) × ( n + 1 ) + 1 (MaxExec-1)\times(n+1)+1 (MaxExec−1)×(n+1)+1
If there are other tasks in the task list A There are other kinds of tasks besides , Then these tasks can be added to the diagram “ Standby status ” The location of , If these tasks can all be arranged in “ Standby status ” among , Then the time required to complete all tasks at this time is the same as before .
Mission A It is the task that needs to be performed the most times , Therefore, there will be no task A More tasks required , But we cannot guarantee that there is no required number of times and tasks A Same task . If this happens , We need to add a position in the last row of the matrix to perform this task .
therefore , If the required number of executions in the task list is equal to M a x E x e c MaxExec MaxExec The number of tasks is n u m num num, Then the time required to complete all tasks is : ( M a x E x e c − 1 ) × ( n + 1 ) + n u m (MaxExec-1)\times(n+1)+num (MaxExec−1)×(n+1)+num
Now we are adding other kinds of tasks to the matrix “ Standby status ” The location of , If the number of times the task needs to be performed is the same as M a x E x e c MaxExec MaxExec equal , Then we need to add the corresponding position in the last row of the matrix . But if in the matrix “ Standby status ” All the positions are filled ? What should I do with the remaining tasks in the task list ?
At this time, we can expand the width of some rows in the matrix , Then insert the task into these extended locations . Because when the matrix is not expanded , The interval between the same tasks in the matrix is exactly n n n, Now that we are in the matrix “ Standby status ” The position of is filled , Then we can insert the remaining tasks directly after the row of the matrix , After inserting, the task in the original matrix does not violate “ The interval between the same tasks is at least n n n ” The provisions of the , And the newly inserted task also meets the cooling requirements .
therefore , If the total number of tasks to be performed in the task list is greater than the size of the matrix we calculated , Then the time required to complete all tasks at this time is the number of tasks in the task list .
Four 、 Code implementation
Although the idea of this topic is complex , But it is still very simple to put this idea into code .
- Count the number of times each task appears in the task list .( Because tasks are expressed in capital letters , So just use a size of 26 Of vector Containers for storage )
- Sort the tasks according to the number of occurrences from more to less .( here vector The first of them 0 The first value is M a x E x e c MaxExec MaxExec Value )
- Count the number of tasks whose occurrence times are equal to the maximum occurrence times .
- Compare the calculated value with the total number of tasks to be performed , Returns a larger value .
The code is as follows :
边栏推荐
- Unity skframework framework (XIV), extension extension function
- 能自动更新的万能周报模板,有手就会用!
- OpenAPI generator: simplify the restful API development process
- 挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
- Unity SKFramework框架(十三)、Question 问题模块
- Daily question: 1175 Prime permutation
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- 阿里发布的Redis开发文档,涵盖了所有的redis操作
- How to modify the error of easydss on demand service sharing time?
- 2、 Frame mode MPLS operation
猜你喜欢

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

EasyDSS点播服务分享时间出错如何修改?

mac(macos Monterey12.2 m1) 个人使用php开发

诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...

Unforgettable Ali, 4 skills, 5 hr additional written tests, it's really difficult and sad to walk

Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf

阿里发布的Redis开发文档,涵盖了所有的redis操作

TVOC, VOC, VOCs gas detection + Solution

(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
![Jerry's watch gets the default ringtone selection list [article]](/img/94/e469864fa6ab688dabe46f606efdbc.jpg)
Jerry's watch gets the default ringtone selection list [article]
随机推荐
Unity SKFramework框架(十八)、RoamCameraController 漫游视角相机控制脚本
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
Jerry's weather code table [chapter]
Redis database persistence
互联网常见34个术语解释
[error record] cannot open "XXX" because Apple cannot check whether it contains malware
Unity SKFramework框架(十九)、POI 兴趣点/信息点
ADB basic commands
JS reverse row query data decryption
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
嵌入式软件开发
Jerry's watch gets the default ringtone selection list [article]
无向图的桥
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
解答:EasyDSS视频点播时音频是否可以设置为默认开启?
[200 opencv routines] 100 Adaptive local noise reduction filter
Daily question: 1175 Prime permutation
What are eNB, EPC and PGW?