Minimum Number of Operations to Make Array Empty Problem
Description
LeetCode Problem 2870.
You are given a 0-indexed array nums consisting of positive integers.
There are two types of operations that you can apply on the array any number of times:
- Choose two elements with equal values and delete them from the array.
- Choose three elements with equal values and delete them from the array.
Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
Example 1:
Input: nums = [2,3,3,2,2,4,2,3,4]
Output: 4
Explanation: We can apply the following operations to make the array empty:
- Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4].
- Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4].
- Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4].
- Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = [].
It can be shown that we cannot make the array empty in less than 4 operations.
Example 2:
Input: nums = [2,1,2,2,3,3]
Output: -1
Explanation: It is impossible to empty the array.
Constraints:
2 <= nums.length <= 10^51 <= nums[i] <= 10^6
Note: This question is the same as 2244: Minimum Rounds to Complete All Tasks.
Solution
Approach: Hash Table + Greedy
Each value is handled independently. Count the occurrences c of every value with a hash table. A value that occurs only once can never be removed, so the answer is -1. Otherwise it is best to remove as many triples as possible, and a remainder of 1 or 2 costs one more operation (for a remainder of 1, swap one triple for two pairs). So the cost is ceil(c / 3), which is (c + 2) / 3 with integer division. Sum it over all values.
Complexity: time O(n), space O(n).
from collections import Counter
class Solution:
def minOperations(self, nums: List[int]) -> int:
count = Counter(nums)
ans = 0
for c in count.values():
if c == 1:
return -1
ans += (c + 2) // 3
return ans