mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>给你一个下标从 <strong>0</strong> 开始的正整数数组 <code>nums</code> 。</p>
 | 
						||
 | 
						||
<p>你可以对数组执行以下两种操作 <strong>任意次</strong> :</p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li>从数组中选择 <strong>两个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li>
 | 
						||
	<li>从数组中选择 <strong>三个</strong> 值 <strong>相等</strong> 的元素,并将它们从数组中 <strong>删除</strong> 。</li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p>请你返回使数组为空的 <strong>最少</strong> 操作次数,如果无法达成,请返回 <code>-1</code> 。</p>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong class="example">示例 1:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<strong>输入:</strong>nums = [2,3,3,2,2,4,2,3,4]
 | 
						||
<b>输出:</b>4
 | 
						||
<b>解释:</b>我们可以执行以下操作使数组为空:
 | 
						||
- 对下标为 0 和 3 的元素执行第一种操作,得到 nums = [3,3,2,4,2,3,4] 。
 | 
						||
- 对下标为 2 和 4 的元素执行第一种操作,得到 nums = [3,3,4,3,4] 。
 | 
						||
- 对下标为 0 ,1 和 3 的元素执行第二种操作,得到 nums = [4,4] 。
 | 
						||
- 对下标为 0 和 1 的元素执行第一种操作,得到 nums = [] 。
 | 
						||
至少需要 4 步操作使数组为空。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p><strong class="example">示例 2:</strong></p>
 | 
						||
 | 
						||
<pre>
 | 
						||
<b>输入:</b>nums = [2,1,2,2,3,3]
 | 
						||
<b>输出:</b>-1
 | 
						||
<b>解释:</b>无法使数组为空。
 | 
						||
</pre>
 | 
						||
 | 
						||
<p> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
 | 
						||
	<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
 | 
						||
</ul>
 |