Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created November 27, 2018 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/896094e66db6c064e75b79fff17a9139 to your computer and use it in GitHub Desktop.
Save fpdjsns/896094e66db6c064e75b79fff17a9139 to your computer and use it in GitHub Desktop.
[leetcode] 945. Minimum Increment to Make Array Unique : https://leetcode.com/problems/minimum-increment-to-make-array-unique/
class Solution {
public:
int minIncrementForUnique(vector<int>& A) {
if(A.size() < 1) return 0;
int ans = 0;
sort(A.begin(), A.end());
int uniqueNum = A[0];
for(int i=1;i<A.size();i++){
uniqueNum++;
if(uniqueNum <= A[i]){
uniqueNum = A[i];
}else{
// A[i] will be uniqueNum
ans += uniqueNum - A[i];
}
}
return ans;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment