Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save coderRohan123/8f42b289327ae0139b7ebb8cc454398b to your computer and use it in GitHub Desktop.
Save coderRohan123/8f42b289327ae0139b7ebb8cc454398b to your computer and use it in GitHub Desktop.
This code snippet calculates the minimum number of operations needed to make all elements in the given list have a frequency that is a multiple of 3. It returns -1 if it is not possible.

Minimum Operations to Make Array Elements Equal

Preview:
class Solution:
    def minOperations(self, nums: List[int]) -> int:
        counter = Counter(nums)
        ans = 0
        for c in counter.values():
            if c == 1: 
                return -1
            ans += ceil(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 ( .py )
Associated Tags Solution class minOperations method nums parameter List[int] list of integers counter variable ans variable for loop if statement ceil function return statement numpy List[int] type hint Counter class
📝 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 take a list of integers as input. It iterates through each integer, and if it is 1, it returns -1; Otherwise, it uses an infinite loop for
This code snippet calculates the minimum number of operations needed to make all elements in the given list have a frequency that is a multiple of 3. It returns -1 if it is not possible.
🔎 Suggested Searches Python code to find minimum operations in a list of integers
Python function to count the number of elements that are equal to 1 and greater than 3 using counter class
How to calculate min operation for an integer array in Python
Python program to determine if there is no difference between 0 and -1 items in a list with one or more values
Code snippet to search for numbers which have not been set by single value
Python code to find minimum operations in a list
Python code to count occurrences in a list and calculate minimum operations
Python code to find minimum operations using Counter in a list
Python code to calculate minimum operations with ceil function in a list
Python code to find minimum operations with Counter and ceil in a list
Related Links https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/solution/
https://leetcode.com/problems/minimum-number-of-operations-to-make-array-empty/discuss/
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://www.geeksforgeeks.org/python-string/
https://www.geeksforgeeks.org/python-lists/
https://www.geeksforgeeks.org/python-functions/
https://www.pythoncentral.io/how-to-sort-a-list-tuple-or-object-with-sorted-in-python/
https://www.pythoncentral.io/how-to-slice-listsarrays-and-tuples-in-python/
https://simpleisbetterthancomplex.com/tutorial/2018/01/18/how-to-implement-multiple-user-types-with-django.html
https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-31.php
https://www.programcreek.com/python/example/109275/collections.Counter
Related People Rohan Mallick
Sensitive Information No Sensitive Information Detected
Shareable Link https://ca772742-b4e2-4612-af82-29a364c1f0bb.pieces.cloud/?p=e2df4a94d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment