LeetCode solutions

3094. Minimum Number of Operations to Make Array Empty

My accepted Python solution to LeetCode problem 3094, Minimum Number of Operations to Make Array Empty, running in 37ms.

  • Difficulty: Medium
  • Python
  • Runtime 37ms
  • Memory 31.6MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 37ms, memory 31.6MB, accepted 2025-12-30.

python
class Solution:
    def minOperations(self, nums: List[int]) -> int:
        from collections import Counter
        
        count = Counter(nums)
        operations = 0
        
        for freq in count.values():
            if freq == 1:
                return -1
            # Use as many 3s as possible, then 2s
            # freq = 3a + 2b, minimize a + b
            # For any freq >= 2, we can always represent it
            # 2 = 2, 3 = 3, 4 = 2+2, 5 = 3+2, 6 = 3+3, etc.
            operations += (freq + 2) // 3
        
        return operations

Source