๐ŸŽฏ CPU Task Scheduler

Imagine you're managing a CPU scheduler that needs to execute tasks efficiently while respecting cooling periods between similar operations. You have an array of tasks, each labeled with a letter from A to Z, and a cooldown number n.

Here's the challenge: identical tasks must be separated by at least n intervals. During each interval, the CPU can either:
โ€ข Execute one task, OR
โ€ข Remain idle (cooling down)

Your mission: Find the minimum number of intervals needed to complete all tasks.

Example: Tasks = ["A","A","A","B","B","B"], n = 2
Optimal schedule: A โ†’ B โ†’ idle โ†’ A โ†’ B โ†’ idle โ†’ A โ†’ B
Total intervals: 8

Input & Output

example_1.py โ€” Basic Case
$ Input: tasks = ["A","A","A","B","B","B"], n = 2
โ€บ Output: 8
๐Ÿ’ก Note: Optimal schedule: A -> B -> idle -> A -> B -> idle -> A -> B. The cooldown period forces idle time between identical tasks.
example_2.py โ€” No Cooldown
$ Input: tasks = ["A","A","A","B","B","B"], n = 0
โ€บ Output: 6
๐Ÿ’ก Note: With no cooldown required, all tasks can be executed consecutively in any order.
example_3.py โ€” Diverse Tasks
$ Input: tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
โ€บ Output: 16
๐Ÿ’ก Note: Task A appears 6 times, creating the framework. Other tasks fill the cooling periods: A->B->C->A->D->E->A->F->G->A->B->C->A->D->E->A.

Visualization

Tap to expand
๐Ÿญ Factory Production SchedulingMachine Types and WorkloadMachine A3 itemsMachine B3 itemsMachine C2 itemsProduction Timeline (n=2 cooling periods)Aโ‚Bโ‚coolingAโ‚‚Bโ‚‚coolingAโ‚ƒBโ‚ƒTotal time slots: 8๐Ÿ’ก Key Insight: Mathematical FormulaMost frequent machines (A, B) need 3 cycles eachFormula: (max_frequency - 1) ร— (cooling_period + 1) + num_max_frequency_machinesResult: (3 - 1) ร— (2 + 1) + 2 = 2 ร— 3 + 2 = 8 time slots
Understanding the Visualization
1
Inventory Count
Count how many items each machine type needs to process
2
Bottleneck Analysis
Identify the machine type with the most work (highest frequency)
3
Framework Design
The busiest machine creates time slots with cooling periods
4
Optimization
Other machines can work during cooling periods of the busiest machine
Key Takeaway
๐ŸŽฏ Key Insight: The most frequent task type creates the scheduling framework - other tasks can fill the cooling periods, making the solution mathematically predictable!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m)

Single pass to count tasks, O(26) to find max frequency

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only need to store 26 task counters (constant space)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค tasks.length โ‰ค 104
  • tasks[i] is an uppercase English letter
  • 0 โ‰ค n โ‰ค 100
  • The number of unique tasks is at most 26
Asked in
Facebook 45 Amazon 38 Google 32 Microsoft 28 Apple 22
62.0K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen