Dodomeki
Sign in

125. Schedule with a Cooldown

Medium

A team must execute a list of tasks. Each task in tarefas is identified by an uppercase letter, and one unit of time allows the team to execute one task. Between two tasks of the same type, at least n units of time must pass; during those units, the team may execute other tasks or remain idle. Find the minimum total number of time units needed to complete all tasks, and return that number.

Example 1
Input: tarefas = ["A", "A", "A", "B", "B", "B"], n = 2
Output: 8
Explanation: The gaps between identical tasks require two units of cooldown.
Example 2
Input: tarefas = ["X", "X", "Y", "Y"], n = 2
Output: 5
Explanation: One idle unit is needed between the two tasks of each type.
Example 3
Input: tarefas = ["A", "B", "C"], n = 5
Output: 3
Explanation: Because no type is repeated, no idle time is needed.
Constraints
  • 0 <= tarefas.length <= 10^4
  • Each element of tarefas is an uppercase letter from A to Z.
  • 0 <= n <= 10^9
GreedyHeap
Caso 1
tarefas =
["A","A","A","B","B","B"]
n =
2
Expected =
8
Caso 2
tarefas =
["X","X","Y","Y"]
n =
2
Expected =
5
Caso 3
tarefas =
["A","B","C"]
n =
5
Expected =
3

Run checks these 3 cases. Submit runs all 13, including hidden ones.