<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:
        
        from collections import Counter

        duplicates = []
        mn, ln = max(nums), len(nums)

        s_nums_dict = Counter(nums)
        s_nums = sorted(s_nums_dict.keys())

        s_nums_dict[mn+ln+1] = 1
        s_nums.append(mn+ln+1)

        prev, curr_idx, duplicates = s_nums[0] - 1, 0, []
        ret_cnt = 0

        while curr_idx<len(s_nums)-1:

            curr = s_nums[curr_idx]

            while curr == prev + 1:

                if s_nums_dict[curr]>1:
                    duplicates.append((curr, s_nums_dict[curr] - 1))
                
                prev = curr
                curr_idx += 1

                if curr_idx>=len(s_nums):
                    break

                curr = s_nums[curr_idx]
            
            if curr_idx<len(s_nums):
                remaining_slots = curr - prev - 1

                while remaining_slots and (duplicates!=[]):
                    val, cnt = duplicates.pop()
                    fill_slots = min( remaining_slots, cnt )
                    cnt -= fill_slots
                    remaining_slots -= fill_slots
                    ret_cnt += (prev - val + 1)*(fill_slots) + (fill_slots-1)*(fill_slots)//2
                    prev = prev + fill_slots

                    if cnt!=0:
                        duplicates.append((val, cnt))
            
                prev = s_nums[curr_idx]-1

        return ret_cnt
34900