Talentd
FleetCode

621. Task Scheduler

Medium64.0% acceptance24 companies

You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of at least n intervals between two tasks with the same label.

Return the minimum number of CPU intervals required to complete all tasks.

Example 1:

Input: tasks = ["A","A","A","B","B","B"], n = 2

Output: 8

Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.

After completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3rd interval, neither A nor B can be done, so you idle. By the 4th interval, you can do A again as 2 intervals have passed.

Example 2:

Input: tasks = ["A","C","A","B","D","B"], n = 1

Output: 6

Explanation: A possible sequence is: A -> B -> C -> D -> A -> B.

With a cooling interval of 1, you can repeat a task after just one other task.

Example 3:

Input: tasks = ["A","A","A", "B","B","B"], n = 3

Output: 10

Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.

There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.

Constraints:

  • 1 <= tasks.length <= 104
  • tasks[i] is an uppercase English letter.
  • 0 <= n <= 100

Hints open one at a time

Hint 1
There are many different solutions for this problem, including a greedy algorithm.
Hint 2
For every cycle, find the most frequent letter that can be placed in this cycle. After placing, decrease the frequency of that letter by one.
Hint 3
Use Priority Queue.
17:07
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def leastInterval(self, tasks: List[str], n: int) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
["A","A","A","B","B","B"]
2
Input
["A","C","A","B","D","B"]
1
Input
["A","A","A", "B","B","B"]
3
Your code is saved on this device.Solution