Minimum increment to make array unique
December 28, 2022
greedyProblem URL: Minimum increment to make array unique
We will sort the array and iterate over the array. If the current element is smaller than the previous element, we will increment the current element to the previous element plus one.
class Solution:
def minIncrementForUnique(self, nums: List[int]) -> int:
nums.sort()
res = 0
for i in range(1, len(nums)):
if nums[i] <= nums[i-1]:
res += nums[i-1] - nums[i] + 1
nums[i] = nums[i-1] + 1
return res
Time complexity: O(nlog(n))
Space complexity: O(1)