621. Task scheduler

You are given a list of tasks that the CPU needs to perform represented by the character array Tasks. Each letter represents a different kind of task. Tasks can be executed in any order, and each task can be completed in one unit of time. At any given unit of time, the CPU can complete a task or be on standby.

However, two tasks of the same kind must have a cooling time of integer length n between them, so that there are at least n consecutive units of time for which the CPU is performing different tasks or is on standby.

You need to calculate the minimum time required to complete all the tasks.

Analysis of the

If the maximum number of executions exceeds n+1, the minimum time is tasks.length

steps

1 First calculate the maximum number of tasks to be executed, and then calculate the number of tasks as large as this type of task

2. Calculate the area of the figure above. The type of the maximum number of executions less than the area is the shortest time, i.e. (maxCount-1)*(n+1) + m

Otherwise, tasks.length is the shortest

var leastInterval = function(tasks, n) { var arr = Array(26).fill(0) for(let i = 0; i< tasks.length; CodePointAt () - 'A'. CodePointAt () arr[v] ++} let Max = math.max (... Arr) let m = 0 for(let I =25; i>0; I -) {if (Max = = arr [I]) {m++}} / / for maximum return math.h Max (Max - 1) * (n + 1) + m, the tasks. The length)}Copy the code