← DiffPush

Task Scheduler

Standard Bar Heaps · Medium Problems O(N) · O(1)

The Broadcast TowerCooldown Plan

A broadcast tower must air a batch of announcements; airing the same announcement twice without n cooling slots in between would violate regulator spacing rules. Each airing takes one slot, and idle slots are permitted. The programming chief wants the minimum number of slots to clear the whole batch legally.

Input: A string tasks where each character is a task type, and an integer n, the cooldown gap between identical tasks.

Output: Return the minimum total number of slots (including idles) needed to run every task.

Constraints

Examples

Example 1

Input: {"tasks":"AAABBB","n":2}
Output: 8
Two max-frequency tasks fill the cooldown frames: (3-1)*3 + 2 = 8 slots.

Example 2

Input: {"tasks":"A","n":2}
Output: 1
A single task needs one slot and no cooldown follows.

Solve this in your browser →

Also on LeetCode ↗