945. Minimum Increment to Make Array Unique
给一个int array: nums,里面的数字会重复,一动可以把一个nums[i] +1
return几动可以让所有数字不重复
丝路:
sort后比较 [i] <= [i-1]?
yes : [i] = [i-1] +1, ans增加 [i] - [i-1] +1
no : 8888
code:
class Solution {
public:
int minIncrementForUnique(vector<int>& nums) {
int ans = 0;
int len = nums.size();
sort(nums.begin(), nums.end());
for(int i = 1; i < len; i++)
{
if(nums[i] <= nums[i-1])
{
ans += (nums[i-1] - nums[i]) + 1;
nums[i] = nums[i-1] + 1;
}
}
return ans;
}
};