<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 {
public:
    int minIncrementForUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        stack<int>suspects;
        int ans = 0;
        //when you find a duplicate, put the 2nd number's index in the suspects stack

                //while you do x--, each time you put a number in the gap (with number of moves = gap index-index of number) increment the gap index by 1.

        //remember you can always put a number at the end of the array.

        //when you get a gap, find the size of the gap. if the size is x, then you can put x many numbers in that gap. use the suspects stack to put a number there.

        int i = 1;
        int last = nums[0];
        for(;i<nums.size(); i++) {
            if(last==nums[i]) {
                suspects.push(nums[i]);
            }

            int gap = nums[i]-last-1;
            int gapIndex = last;
            while(gap>0 && !suspects.empty()) {
                ans+=gapIndex-suspects.top()+1;
                suspects.pop();
                gapIndex++;
                gap--;
            }
            last = nums[i];
        }

        while(!suspects.empty()) {
            ans+=last-suspects.top()+1;
            suspects.pop();
            last++;
        }
        return ans;
        




        // int suspects, moves = 0;
        // for(auto c:nums) cout<<c<<" ";
        // cout<<"\n";
        // for(int i = 1; i<nums.size(); i++) {
        //     cout<<nums[i-1]<<" "<<nums[i]<<endl;
        //     if(nums[i]==nums[i-1]) {
        //         nums[i]++;
        //         moves++;
        //     }
        // }
        // return moves;
    }
};
70383