Skip to content
LC-0945 Medium LeetCode

945. Minimum Increment to Make Array Unique

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 60% Topics: Array, Greedy, Sorting, Counting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(nlogn)
# Space: O(n)

class Solution(object):
    def minIncrementForUnique(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        A.sort()
        A.append(float("inf"))
        result, duplicate = 0, 0
        for i in xrange(1, len(A)):
            if A[i-1] == A[i]:
                duplicate += 1
                result -= A[i]
            else:
                move = min(duplicate, A[i]-A[i-1]-1)
                duplicate -= move
                result += move*A[i-1] + move*(move+1)//2
        return result

Solution from kamyu104/LeetCode-Solutions · MIT