|
| 1 | +<h2><a href="https://leetcode.com/problems/task-scheduler">Task Scheduler</a></h2> <img src='https://img.shields.io/badge/Difficulty-Medium-orange' alt='Difficulty: Medium' /><hr><p>You are given an array of CPU <code>tasks</code>, each represented by letters A to Z, and a cooling time, <code>n</code>. Each cycle or interval allows the completion of one task. Tasks can be completed in any order, but there's a constraint: <strong>identical</strong> tasks must be separated by at least <code>n</code> intervals due to cooling time.</p> |
| 2 | + |
| 3 | +<p>Return the <em>minimum number of intervals</em> required to complete all tasks.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong class="example">Example 1:</strong></p> |
| 7 | + |
| 8 | +<div class="example-block" style=" |
| 9 | + border-color: var(--border-tertiary); |
| 10 | + border-left-width: 2px; |
| 11 | + color: var(--text-secondary); |
| 12 | + font-size: .875rem; |
| 13 | + margin-bottom: 1rem; |
| 14 | + margin-top: 1rem; |
| 15 | + overflow: visible; |
| 16 | + padding-left: 1rem; |
| 17 | +"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io" style=" |
| 19 | + font-family: Menlo,sans-serif; |
| 20 | + font-size: 0.85rem; |
| 21 | +">tasks = ["A","A","A","B","B","B"], n = 2</span></p> |
| 22 | + |
| 23 | +<p><strong>Output:</strong> <span class="example-io" style=" |
| 24 | +font-family: Menlo,sans-serif; |
| 25 | +font-size: 0.85rem; |
| 26 | +">8</span></p> |
| 27 | + |
| 28 | +<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.</p> |
| 29 | + |
| 30 | +<p>After completing task A, you must wait two cycles before doing A again. The same applies to task B. In the 3<sup>rd</sup> interval, neither A nor B can be done, so you idle. By the 4<sup>th</sup> cycle, you can do A again as 2 intervals have passed.</p> |
| 31 | +</div> |
| 32 | + |
| 33 | +<p><strong class="example">Example 2:</strong></p> |
| 34 | + |
| 35 | +<div class="example-block" style=" |
| 36 | + border-color: var(--border-tertiary); |
| 37 | + border-left-width: 2px; |
| 38 | + color: var(--text-secondary); |
| 39 | + font-size: .875rem; |
| 40 | + margin-bottom: 1rem; |
| 41 | + margin-top: 1rem; |
| 42 | + overflow: visible; |
| 43 | + padding-left: 1rem; |
| 44 | +"> |
| 45 | +<p><strong>Input:</strong> <span class="example-io" style=" |
| 46 | + font-family: Menlo,sans-serif; |
| 47 | + font-size: 0.85rem; |
| 48 | +">tasks = ["A","C","A","B","D","B"], n = 1</span></p> |
| 49 | + |
| 50 | +<p><strong>Output:</strong> <span class="example-io" style=" |
| 51 | + font-family: Menlo,sans-serif; |
| 52 | + font-size: 0.85rem; |
| 53 | +">6</span></p> |
| 54 | + |
| 55 | +<p><strong>Explanation:</strong> A possible sequence is: A -> B -> C -> D -> A -> B.</p> |
| 56 | + |
| 57 | +<p>With a cooling interval of 1, you can repeat a task after just one other task.</p> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p><strong class="example">Example 3:</strong></p> |
| 61 | + |
| 62 | +<div class="example-block" style=" |
| 63 | + border-color: var(--border-tertiary); |
| 64 | + border-left-width: 2px; |
| 65 | + color: var(--text-secondary); |
| 66 | + font-size: .875rem; |
| 67 | + margin-bottom: 1rem; |
| 68 | + margin-top: 1rem; |
| 69 | + overflow: visible; |
| 70 | + padding-left: 1rem; |
| 71 | +"> |
| 72 | +<p><strong>Input:</strong> <span class="example-io" style=" |
| 73 | + font-family: Menlo,sans-serif; |
| 74 | + font-size: 0.85rem; |
| 75 | +">tasks = ["A","A","A", "B","B","B"], n = 3</span></p> |
| 76 | + |
| 77 | +<p><strong>Output:</strong> <span class="example-io" style=" |
| 78 | + font-family: Menlo,sans-serif; |
| 79 | + font-size: 0.85rem; |
| 80 | +">10</span></p> |
| 81 | + |
| 82 | +<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.</p> |
| 83 | + |
| 84 | +<p>There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.</p> |
| 85 | +</div> |
| 86 | + |
| 87 | +<p> </p> |
| 88 | +<p><strong>Constraints:</strong></p> |
| 89 | + |
| 90 | +<ul> |
| 91 | + <li><code>1 <= tasks.length <= 10<sup>4</sup></code></li> |
| 92 | + <li><code>tasks[i]</code> is an uppercase English letter.</li> |
| 93 | + <li><code>0 <= n <= 100</code></li> |
| 94 | +</ul> |
0 commit comments