mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			61 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			61 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>You are given a collection of numbered <code>balls</code> and instructed to sort them into boxes for a nearly balanced distribution. There are two rules you must follow:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li>Balls with the same box must have the same value. But, if you have more than one ball with the same number, you can put them in different boxes.</li>
 | 
						|
	<li>The biggest box can only have one more ball than the smallest box.</li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p>Return the <em>fewest number of boxes</em> to sort these balls following these rules.</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;"> balls = [3,2,3,2,3] </span></p>
 | 
						|
 | 
						|
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 2 </span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>We can sort <code>balls</code> into boxes as follows:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>[3,3,3]</code></li>
 | 
						|
	<li><code>[2,2]</code></li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p>The size difference between the two boxes doesn't exceed one.</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;"> balls = [10,10,10,3,1,1] </span></p>
 | 
						|
 | 
						|
<p><strong>Output: </strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 4 </span></p>
 | 
						|
 | 
						|
<p><strong>Explanation:</strong></p>
 | 
						|
 | 
						|
<p>We can sort <code>balls</code> into boxes as follows:</p>
 | 
						|
 | 
						|
<ul>
 | 
						|
</ul>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>[10]</code></li>
 | 
						|
	<li><code>[10,10]</code></li>
 | 
						|
	<li><code>[3]</code></li>
 | 
						|
	<li><code>[1,1]</code></li>
 | 
						|
</ul>
 | 
						|
 | 
						|
<p>You can't use fewer than four boxes while still following the rules. For example, putting all three balls numbered 10 in one box would break the rule about the maximum size difference between boxes.</p>
 | 
						|
</div>
 | 
						|
 | 
						|
<p> </p>
 | 
						|
<p><strong>Constraints:</strong></p>
 | 
						|
 | 
						|
<ul>
 | 
						|
	<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
 | 
						|
	<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
 | 
						|
</ul>
 |