All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 14, 2024
Last updated : July 01, 2024
Related Topics : Array, Greedy, Sorting, Counting
Acceptance Rate : 60.09 %
class Solution:
def minIncrementForUnique(self, nums: List[int]) -> int:
nums.sort()
prevVals = set()
maxVal = -inf
output = 0
for num in nums :
if num in prevVals :
maxVal = maxVal + 1
output += maxVal - num
prevVals.add(maxVal)
else :
maxVal = num
prevVals.add(num)
return output