1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-02-04 06:30:27 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/使数组元素互不相同所需的最少操作次数 [minimum-number-of-operations-to-make-elements-in-array-distinct].html
2025-01-09 01:30:35 +08:00

67 lines
2.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个整数数组 <code>nums</code>,你需要确保数组中的元素&nbsp;<strong>互不相同&nbsp;</strong>。为此,你可以执行以下操作任意次:</p>
<ul>
<li>从数组的开头移除 3 个元素。如果数组中元素少于 3 个,则移除所有剩余元素。</li>
</ul>
<p><strong>注意:</strong>空数组也视作为数组元素互不相同。返回使数组元素互不相同所需的&nbsp;<strong>最少操作次数&nbsp;</strong><!-- notionvc: 210ee4f2-90af-4cdf-8dbc-96d1fa8f67c7 --></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p>
<p><strong>输出:</strong> <span class="example-io">2</span></p>
<p><strong>解释:</strong></p>
<ul>
<li>第一次操作:移除前 3 个元素,数组变为 <code>[4, 2, 3, 3, 5, 7]</code></li>
<li>第二次操作:再次移除前 3 个元素,数组变为 <code>[3, 5, 7]</code>,此时数组中的元素互不相同。</li>
</ul>
<p>因此,答案是 2。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p>
<p><strong>输出:</strong> <span class="example-io">2</span></p>
<p><strong>解释:</strong></p>
<ul>
<li>第一次操作:移除前 3 个元素,数组变为 <code>[4, 4]</code></li>
<li>第二次操作:移除所有剩余元素,数组变为空。</li>
</ul>
<p>因此,答案是 2。</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block">
<p><strong>输入:</strong> <span class="example-io">nums = [6,7,8,9]</span></p>
<p><strong>输出:</strong> <span class="example-io">0</span></p>
<p><strong>解释:</strong></p>
<p>数组中的元素已经互不相同,因此不需要进行任何操作,答案是 0。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
</ul>