class Solution {
public int minOperations(int[] nums) {
var counter = new HashMap<Integer, Integer>();
for (int num: nums) {
counter.put(num, counter.getOrDefault(num, 0) + 1);
}
int ans = 0;
for (int c: counter.values()) {
if (c == 1) {
return -1;
}
ans += Math.ceil((double) c / 3);
}
return ans;
}
}
/*
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.*/| Associated Context | |
|---|---|
| Type | Code Snippet ( .dart ) |
| Associated Tags | minOperations method int[] nums parameter HashMap<Integer Integer> getOrDefault method Math ceil function counter variable ceil method values method Solution class for loop if statement return statement nums parameter HashMap ans variable |
| 📝 Custom Description | Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. |
| 💡 Smart Description | This code snippet calculates the minimum number of operations required to perform a given operation on an array. It uses a HashMap to keep track of each element and then iterates through all elements, calculating their sum by 3 in descending order based on This code snippet takes an array of integers as input and calculates the minimum number of operations required to make all elements in the array equal. It uses a HashMap to count the frequency of each number, and then iterates through the counts to calculate the minimum |
| 🔎 Suggested Searches | Java code to find minimum operations in an array HashMap usage for finding minimum operation in Java How to calculate the number of operations needed by a given array in Java Java program to count maximum elements using counter and summing values Algorithm to determine if there are no more than one element in java HashMap implementation to count occurrences in an array Java code to calculate ceil of a division Code to find unique elements in an array using HashMap Java code to calculate sum of values in a HashMap |
| Related Links | https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/submissions/ https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/discuss/?currentPage=1&orderBy=hot&query= https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/ https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/solution/ https://www.geeksforgeeks.org/java-util-hashmap-in-java-with-examples/ https://www.programiz.com/java-programming/library/hashmap/getordefault https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/HashMap.html https://www.geeksforgeeks.org/ https://www.geeksforgeeks.org/data-structures/linked-list/ https://www.geeksforgeeks.org/what-is-linked-list/ https://www.w3schools.com/java/java_hashmap.asp https://www.baeldung.com/java-hashmap https://www.javatpoint.com/java-hashmap |
| Related People | Rohan Mallick |
| Sensitive Information | No Sensitive Information Detected |
| Shareable Link | No Shareable Link |