|
| 1 | +<p>You are given an integer array <code>nums</code>. You need to ensure that the elements in the array are <strong>distinct</strong>. To achieve this, you can perform the following operation any number of times:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>Remove 3 elements from the beginning of the array. If the array has fewer than 3 elements, remove all remaining elements.</li> |
| 5 | +</ul> |
| 6 | + |
| 7 | +<p><strong>Note</strong> that an empty array is considered to have distinct elements. Return the <strong>minimum</strong> number of operations needed to make the elements in the array distinct.<!-- notionvc: 210ee4f2-90af-4cdf-8dbc-96d1fa8f67c7 --></p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,4,2,3,3,5,7]</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 2, 3, 3, 5, 7]</code>.</li> |
| 21 | + <li>In the second operation, the next 3 elements are removed, resulting in the array <code>[3, 5, 7]</code>, which has distinct elements.</li> |
| 22 | +</ul> |
| 23 | + |
| 24 | +<p>Therefore, the answer is 2.</p> |
| 25 | +</div> |
| 26 | + |
| 27 | +<p><strong class="example">Example 2:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,5,6,4,4]</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> 2</p> |
| 33 | + |
| 34 | +<p><strong>Explanation:</strong></p> |
| 35 | + |
| 36 | +<ul> |
| 37 | + <li>In the first operation, the first 3 elements are removed, resulting in the array <code>[4, 4]</code>.</li> |
| 38 | + <li>In the second operation, all remaining elements are removed, resulting in an empty array.</li> |
| 39 | +</ul> |
| 40 | + |
| 41 | +<p>Therefore, the answer is 2.</p> |
| 42 | +</div> |
| 43 | + |
| 44 | +<p><strong class="example">Example 3:</strong></p> |
| 45 | + |
| 46 | +<div class="example-block"> |
| 47 | +<p><strong>Input:</strong> <span class="example-io">nums = [6,7,8,9]</span></p> |
| 48 | + |
| 49 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 50 | + |
| 51 | +<p><strong>Explanation:</strong></p> |
| 52 | + |
| 53 | +<p>The array already contains distinct elements. Therefore, the answer is 0.</p> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>1 <= nums.length <= 100</code></li> |
| 61 | + <li><code>1 <= nums[i] <= 100</code></li> |
| 62 | +</ul> |
0 commit comments