2870. Minimum Number of Operations to Make Array Empty LeetCode Solution
In this guide, you will get 2870. Minimum Number of Operations to Make Array Empty LeetCode Solution with the best time and space complexity. The solution to Minimum Number of Operations to Make Array Empty problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Number of Operations to Make Array Empty solution in C++
- Minimum Number of Operations to Make Array Empty solution in Java
- Minimum Number of Operations to Make Array Empty solution in Python
- Additional Resources
Problem Statement of Minimum Number of Operations to Make Array Empty
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 <= 105
1 <= nums[i] <= 106
Note: This question is the same as 2244: Minimum Rounds to Complete All Tasks.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2870. Minimum Number of Operations to Make Array Empty LeetCode Solution in C++
class Solution {
public:
int minOperations(vector<int>& nums) {
int ans = 0;
unordered_map<int, int> count;
for (const int num : nums)
++count[num];
for (const auto& [_, freq] : count) {
// If freq == 3k, need k operations.
// If freq == 3k + 1 = 3*(k - 1) + 2*2, need k + 1 operations.
// If freq == 3k + 2, need k + 1 operations.
if (freq == 1)
return -1;
ans += (freq + 2) / 3;
}
return ans;
}
};
/* code provided by PROGIEZ */
2870. Minimum Number of Operations to Make Array Empty LeetCode Solution in Java
class Solution {
public int minOperations(int[] nums) {
int ans = 0;
Map<Integer, Integer> count = new HashMap<>();
for (final int num : nums)
count.merge(num, 1, Integer::sum);
for (final int freq : count.values()) {
// If freq == 3k, need k operations.
// If freq == 3k + 1 = 3*(k - 1) + 2*2, need k + 1 operations.
// If freq == 3k + 2, need k + 1 operations.
if (freq == 1)
return -1;
ans += (freq + 2) / 3;
}
return ans;
}
}
// code provided by PROGIEZ
2870. Minimum Number of Operations to Make Array Empty LeetCode Solution in Python
class Solution:
def minOperations(self, nums: list[int]) -> int:
count = collections.Counter(nums)
if 1 in count.values():
return -1
return sum((freq + 2) // 3 for freq in count.values())
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.