<p>You are given an integer array <code>nums</code>. In one move, you can pick an index <code>i</code> where <code>0 &lt;= i &lt; nums.length</code> and increment <code>nums[i]</code> by <code>1</code>.</p>

<p>Return <em>the minimum number of moves to make every value in </em><code>nums</code><em> <strong>unique</strong></em>.</p>

<p>The test cases are generated so that the answer fits in a 32-bit integer.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> After 1 move, the array could be [1, 2, 3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1,2,1,7]
<strong>Output:</strong> 6
<strong>Explanation:</strong> After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def minIncrementForUnique(self, nums: List[int]) -> int:
        #the fact this is an array is irrelevant, we just have a bunch of numbers that must be unique
        moves = 0
        N = len(nums)
        counts = [0] * (10**5+1)
        mini = 10**5 + 1
        maxi = -1
        #remap = dict()
        for n in nums:
            # optimization if sparse
            #if n > N:
            #    if n not in remap:
            #        remap[n] = maxi + N
            #    n = remap[n]
            ##
            counts[n] += 1
            mini = min(mini,n)
            maxi = max(maxi,n)
        surplus = []
        for n in range(mini,maxi+1):
            if counts[n] > 1:
                surplus += [n] * (counts[n] - 1)
            elif counts[n] == 0:
                if len(surplus) > 0:
                    moves += n-surplus.pop()
        for i,s in enumerate(surplus):
            moves += (maxi+i+1) - s
        return moves


        
31400