forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
30 lines (30 loc) · 819 Bytes
/
solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int minIncrementForUnique(vector<int>& A) {
if(A.size() == 0)
return 0;
sort(A.begin(),A.end());
vector<int> n;
int last = A[0];
int move = 0;
for(int i=1;i<A.size();i++){
if(A[i] == last)
n.push_back(A[i]);
if(A[i] - last > 1){
int d = A[i] - last - 1;
for(int j=0;j<d && n.size()>0;j++){
last = last + 1;
move += last - n[n.size()-1];
n.erase(n.end()-1);
}
}
last = A[i];
}
for(int i=0;i<n.size();i++){
last = last + 1;
move += last - n[i];
// cout<<last<<endl;
}
return move;
}
};