|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/">2870. Minimum Number of Operations to Make Array Empty</a></h2><h3>Medium</h3><hr><div><p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers.</p> |
| 2 | + |
| 3 | +<p>There are two types of operations that you can apply on the array <strong>any</strong> number of times:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>Choose <strong>two</strong> elements with <strong>equal</strong> values and <strong>delete</strong> them from the array.</li> |
| 7 | + <li>Choose <strong>three</strong> elements with <strong>equal</strong> values and <strong>delete</strong> them from the array.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>Return <em>the <strong>minimum</strong> number of operations required to make the array empty, or </em><code>-1</code><em> if it is not possible</em>.</p> |
| 11 | + |
| 12 | +<p> </p> |
| 13 | +<p><strong class="example">Example 1:</strong></p> |
| 14 | + |
| 15 | +<pre><strong>Input:</strong> nums = [2,3,3,2,2,4,2,3,4] |
| 16 | +<strong>Output:</strong> 4 |
| 17 | +<strong>Explanation:</strong> We can apply the following operations to make the array empty: |
| 18 | +- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. |
| 19 | +- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. |
| 20 | +- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. |
| 21 | +- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. |
| 22 | +It can be shown that we cannot make the array empty in less than 4 operations. |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<pre><strong>Input:</strong> nums = [2,1,2,2,3,3] |
| 28 | +<strong>Output:</strong> -1 |
| 29 | +<strong>Explanation:</strong> It is impossible to empty the array. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p> </p> |
| 33 | +<p><strong>Constraints:</strong></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li><code>2 <= nums.length <= 10<sup>5</sup></code></li> |
| 37 | + <li><code>1 <= nums[i] <= 10<sup>6</sup></code></li> |
| 38 | +</ul> |
| 39 | +</div> |
0 commit comments