945. Minimum Increment to Make Array Unique

Given an array of integers A, a move consists of choosing any `A[i]`, and incrementing it by `1`.

Return the least number of moves to make every value in `A` unique.

Example 1:

```Input: [1,2,2]
Output: 1
Explanation:  After 1 move, the array could be [1, 2, 3].
```

Example 2:

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

Note:

1. `0 <= A.length <= 40000`
2. `0 <= A[i] < 40000`

945. Minimum Increment to Make Array Unique
``````struct Solution;

impl Solution {
fn min_increment_for_unique(mut a: Vec<i32>) -> i32 {
let mut res = 0;
a.sort_unstable();
let mut prev: Option<i32> = None;
for x in a {
if let Some(y) = prev {
if x <= y {
res += y + 1 - x;
prev = Some(y + 1);
} else {
prev = Some(x);
}
} else {
prev = Some(x);
}
}
res
}
}

#[test]
fn test() {
let a = vec![1, 2, 2];
let res = 1;
assert_eq!(Solution::min_increment_for_unique(a), res);
let a = vec![3, 2, 1, 2, 1, 7];
let res = 6;
assert_eq!(Solution::min_increment_for_unique(a), res);
}
``````