mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 11:43:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			95 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			3.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You are given an array of CPU <code>tasks</code>, each labeled with a letter from A to Z, and a number <code>n</code>. 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 <strong>at least</strong> <code>n</code> intervals between two tasks with the same label.</p>
 | 
						|
 | 
						|
<p>Return the <strong>minimum</strong> number of CPU intervals required to complete all tasks.</p>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong class="example">Example 1:</strong></p>
 | 
						|
 | 
						|
<div class="example-block" style="
 | 
						|
    border-color: var(--border-tertiary);
 | 
						|
    border-left-width: 2px;
 | 
						|
    color: var(--text-secondary);
 | 
						|
    font-size: .875rem;
 | 
						|
    margin-bottom: 1rem;
 | 
						|
    margin-top: 1rem;
 | 
						|
    overflow: visible;
 | 
						|
    padding-left: 1rem;
 | 
						|
">
 | 
						|
<p><strong>Input:</strong> <span class="example-io" style="
 | 
						|
    font-family: Menlo,sans-serif;
 | 
						|
    font-size: 0.85rem;
 | 
						|
">tasks = ["A","A","A","B","B","B"], n = 2</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io" style="
 | 
						|
font-family: Menlo,sans-serif;
 | 
						|
font-size: 0.85rem;
 | 
						|
">8</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.</p>
 | 
						|
 | 
						|
<p>After completing task A, you must wait two intervals 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> interval, you can do A again as 2 intervals have passed.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 2:</strong></p>
 | 
						|
 | 
						|
<div class="example-block" style="
 | 
						|
    border-color: var(--border-tertiary);
 | 
						|
    border-left-width: 2px;
 | 
						|
    color: var(--text-secondary);
 | 
						|
    font-size: .875rem;
 | 
						|
    margin-bottom: 1rem;
 | 
						|
    margin-top: 1rem;
 | 
						|
    overflow: visible;
 | 
						|
    padding-left: 1rem;
 | 
						|
">
 | 
						|
<p><strong>Input:</strong> <span class="example-io" style="
 | 
						|
    font-family: Menlo,sans-serif;
 | 
						|
    font-size: 0.85rem;
 | 
						|
">tasks = ["A","C","A","B","D","B"], n = 1</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io" style="
 | 
						|
    font-family: Menlo,sans-serif;
 | 
						|
    font-size: 0.85rem;
 | 
						|
">6</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> C -> D -> A -> B.</p>
 | 
						|
 | 
						|
<p>With a cooling interval of 1, you can repeat a task after just one other task.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p><strong class="example">Example 3:</strong></p>
 | 
						|
 | 
						|
<div class="example-block" style="
 | 
						|
    border-color: var(--border-tertiary);
 | 
						|
    border-left-width: 2px;
 | 
						|
    color: var(--text-secondary);
 | 
						|
    font-size: .875rem;
 | 
						|
    margin-bottom: 1rem;
 | 
						|
    margin-top: 1rem;
 | 
						|
    overflow: visible;
 | 
						|
    padding-left: 1rem;
 | 
						|
">
 | 
						|
<p><strong>Input:</strong> <span class="example-io" style="
 | 
						|
    font-family: Menlo,sans-serif;
 | 
						|
    font-size: 0.85rem;
 | 
						|
">tasks = ["A","A","A", "B","B","B"], n = 3</span></p>
 | 
						|
 | 
						|
<p><strong>Output:</strong> <span class="example-io" style="
 | 
						|
    font-family: Menlo,sans-serif;
 | 
						|
    font-size: 0.85rem;
 | 
						|
">10</span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.</p>
 | 
						|
 | 
						|
<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>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= tasks.length <= 10<sup>4</sup></code></li>
 | 
						|
	<li><code>tasks[i]</code> is an uppercase English letter.</li>
 | 
						|
	<li><code>0 <= n <= 100</code></li>
 | 
						|
</ul>
 |