Skip to content
LC-3396 Easy LeetCode

3396. Minimum Number of Operations to Make Elements in Array Distinct

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 72% Topics: Array, Hash Table
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(n + r)
# Space: O(r)

# freq table
class Solution(object):
    def minimumOperations(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        def ceil_divide(a, b):
            return (a+b-1)//b

        mx = max(nums)
        cnt = [0]*mx
        for i in reversed(xrange(len(nums))):
            cnt[nums[i]-1] += 1
            if cnt[nums[i]-1] == 2:
                return ceil_divide(i+1, 3)
        return 0

Solution from kamyu104/LeetCode-Solutions · MIT