:orphan: Task Scheduler ============== .. highlight:: none Problem ------- https://leetcode.com/problems/task-scheduler/ You are given an array of CPU tasks, each represented by letters ``A`` to ``Z``, and a cooling time, ``n``. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: **identical tasks must be separated by at least** ``n`` **intervals** due to cooling time. Return the *minimum number of intervals* the CPU will take to finish all the given tasks. **Example 1:** :: Input: tasks = ["A","A","A","B","B","B"], n = 2 Output: 8 Explanation: A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B. **Example 2:** :: Input: tasks = ["A","C","A","B","D","B"], n = 1 Output: 6 Explanation: A possible sequence is: A -> B -> C -> D -> A -> B. **Example 3:** :: Input: tasks = ["A","A","A","B","B","B"], n = 3 Output: 10 Explanation: A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B. **Constraints:** - ``1 <= tasks.length <= 10``\ :sup:`4` - ``tasks[i]`` is an uppercase English letter. - ``0 <= n <= 100`` .. highlight:: python Pattern ------- Heap, Greedy, Hash Table Approaches ---------- .. tab-set:: .. tab-item:: Greedy **Code** .. literalinclude:: ../problems/medium/task-scheduler/task_scheduler__greedy.py :language: python :lines: 11- **Test** >>> from task_scheduler__greedy import leastInterval >>> leastInterval(["A","A","A","B","B","B"], 2) 8 >>> leastInterval(["A","C","A","B","D","B"], 1) 6 >>> leastInterval(["A","A","A","B","B","B"], 3) 10 .. autofunction:: task_scheduler__greedy.leastInterval .. tab-item:: Max Heap **Code** .. literalinclude:: ../problems/medium/task-scheduler/task_scheduler__max_heap.py :language: python :lines: 11- **Test** >>> from task_scheduler__max_heap import leastInterval >>> leastInterval(["A","A","A","B","B","B"], 2) 8 >>> leastInterval(["A","C","A","B","D","B"], 1) 6 >>> leastInterval(["A","A","A","B","B","B"], 3) 10 .. autofunction:: task_scheduler__max_heap.leastInterval