Task Scheduler: the idea to remember.
Find the fewest unit intervals needed to run uppercase tasks when equal tasks require n intervening intervals.
The core insight
The most frequent tasks define cooldown frames; tied maximum frequencies occupy the last frame.
Why it works
No schedule can beat either total tasks or the required spacing of the most frequent tasks, and filling gaps reaches that bound.
The mistake to avoid
Idle intervals are needed only when other tasks cannot fill the gaps; account for ties at the maximum frequency.
Commented Java solution
Use java.util.* imports and a surrounding solution class. Tree snippets assume the standard TreeNode definition; linked lists use ListNode and Clone Graph uses a Node with val and neighbors; Copy Random List uses Node with val, next and random. Read the input assumptions above.
int leastInterval(char[] tasks, int n) {
int[] count = new int[26];
int most = 0, ties = 0;
for (char task : tasks) most = Math.max(most, ++count[task - 'A']);
for (int frequency : count) if (frequency == most) ties++;
// The most frequent tasks create frames of n + 1 slots.
return Math.max(tasks.length, (most - 1) * (n + 1) + ties);
}
Make the next connection
Independent revision notes with credited Java references. Not affiliated with LeetCode or the source sheets.