15% off all plans & roadmaps Use code
See plansLeetCode 621 · Solution & editorial
You are given an array of CPU tasks, each labeled with a letter from A to Z, and a number n. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of at least n intervals between two tasks with the same label.
Task Scheduler is a Greedy Patterns problem, in the Task Scheduling (Frequency Based) subpattern. The pattern covers interval scheduling, jump games, stock trading, and task scheduling
Schedule tasks with cooldown using frequency-based greedy approach.
The reference solution runs in O(N) time and O(1) space.
Task Scheduler has been reported in interviews at Facebook, Rubrik, Amazon, Pinterest, Google, Microsoft, Bloomberg, Airtel.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Task Scheduler is a Greedy Patterns problem, in the Task Scheduling (Frequency Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.