I started from index 0 and checked for duplicates using a frequency counter. If duplicates existed, I removed the first 3 elements (or all if fewer than 3) and counted one operation. I repeated this until all remaining elements were distinct, or the array was empty.
class Solution:
def minimumOperations(self, nums: List[int]) -> int:
ops = 0
while True:
freq = Counter(nums)
if all(v == 1 for v in freq.values()):
break
nums = nums[3:] if len(nums) >= 3 else []
ops += 1
return ops
- Time: O(n^2)
- Space: O(1)
