LeetCode 621 Task Scheduler - Example and Complexity Analysis

Walkthrough and analysis

Trace ["A","A","A","B","B","B"] with n = 2.

Count: A=3, B=3. maxCount = 3. Two tasks have max count.

Frame structure: (maxCount - 1) groups of (n + 1) slots, then a final batch.

(3 - 1) × (2 + 1) = 6 slots for the first 2 occurrences of max-frequency tasks. Plus 2 tasks in the final batch (A and B both have 3 occurrences).

Total: 6 + 2 = 8.

Schedule: A B _ | A B _ | A B where | shows frame boundaries and _ is idle.

If total tasks > this formula result, return total tasks (no idle needed).

Count frequencies: O(n)O(n). Find max: O(1)O(1) if using fixed-size array for 26 letters. Total: O(n)O(n) time. O(1)O(1) space.