https://leetcode.com/problems/minimum-increment-to-make-array-unique/
Approach 2: Maintain Duplicate Info
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:
0 <= A.length <= 40000
0 <= A[i] < 40000
Sort the array.
Compared with previous number,
the current number need to be at least prev + 1.
Time Complexity: O(NlogN)
Compared with previous number,
the current number need to be at least prev + 1.
Time Complexity: O(NlogN)
public int minIncrementForUnique(int[] A) {
Arrays.sort(A);
int res = 0, need = 0;
for (int a : A) {
res += Math.max(need - a, 0);
need = Math.max(a, need) + 1;
}
return res;
}
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/197713/C%2B%2B-concise-solution-O(nlogn)-complexity-with-explanation-and-example-step-by-step
The idea is to sort the input -O(nlogn)- , then we move forward from the beginning of the array till the end.
As soon as we found a condition that the current element is less than or equal to the previous elements then we need to update the current array element.
here is an example of the given input.
A = [3,2,1,2,1,7]
Sorted A = [1,1,2,2,3,7]
After reaching the second 1 on the array since the condition is satisfied A[i]<=A[i-1] so we need to update the A[i] by A[i-1]+1.
At the same time wee need to keep track of result by
result += A[i-1]+ 1 - A[i];
As soon as we found a condition that the current element is less than or equal to the previous elements then we need to update the current array element.
here is an example of the given input.
A = [3,2,1,2,1,7]
Sorted A = [1,1,2,2,3,7]
After reaching the second 1 on the array since the condition is satisfied A[i]<=A[i-1] so we need to update the A[i] by A[i-1]+1.
At the same time wee need to keep track of result by
result += A[i-1]+ 1 - A[i];
The rest of iterations are as following :
A = [1,2,2,2,3,7]
res= 1
res= 1
A = [1,2,3,2,3,7]
res= 2
res= 2
A = [1,2,3,4,3,7]
res= 4
res= 4
A = [1,2,3,4,5,7]
res= 6
res= 6
int minIncrementForUnique(vector<int>& A) {
int s = A.size();
int res=0;
if (s<2) return 0;
sort(A.begin(),A.end());
for (int i=1; i<s; ++i) {
if (A[i]<=A[i-1]){
res+=A[i-1]+1 -A[i];
A[i]= A[i-1]+1;
}
}
return res;
}
Approach 2: Maintain Duplicate Info
Let's imagine the array is sorted and we are moving from left to right. As in Approach 1, we want to take duplicate values to release later.
Algorithm
There are two cases.
- If
A[i-1] == A[i]
, we have a duplicate to take. - If
A[i-1] < A[i]
, we might be able to place our taken values into those free positions. Specifically, we havegive = min(taken, A[i] - A[i-1] - 1)
possible values to release, and they will have final valuesA[i-1] + 1, A[i-1] + 2, ..., A[i-1] + give
. This has a sum of .
- Time Complexity: , where is the length of
A
. - Space Complexity: in additional space complexity, depending on the specific implementation of the built in sort.
public int minIncrementForUnique(int[] A) {
Arrays.sort(A);
int ans = 0, taken = 0;
for (int i = 1; i < A.length; ++i) {
if (A[i - 1] == A[i]) {
taken++;
ans -= A[i];
} else {
int give = Math.min(taken, A[i] - A[i - 1] - 1);
ans += give * (give + 1) / 2 + give * A[i - 1];
taken -= give;
}
}
if (A.length > 0)
ans += taken * (taken + 1) / 2 + taken * A[A.length - 1];
return ans;
}
https://leetcode.com/problems/minimum-increment-to-make-array-unique/discuss/198215/Java-O(n-%2B-m)-solution-without-sort
Let's count the quantity of each element. Clearly, we want to increment duplicated values.
For each duplicate value, we could do a "brute force" solution of incrementing it repeatedly until it is not unique. However, we might do a lot of work - consider the work done by an array of all ones. We should think of how to amend our solution to solve this case as well.
What we can do instead is lazily evaluate our increments. If for example we have
[1, 1, 1, 1, 3, 5]
, we don't need to process all the increments of duplicated 1
s. We could take three ones (taken = [1, 1, 1]
) and continue processing. When we find an empty place like 2
, 4
, or 6
, we can then recover that our increment will be 2-1
, 4-1
, and 6-1
.
Algorithm
Count the values. For each possible value
x
:- If there are 2 or more values
x
inA
, save the extra duplicated values to increment later. - If there are 0 values
x
inA
, then a saved valuev
gets incremented tox
.
In Java, the code is less verbose with a slight optimization: we record only the number of saved values, and we subtract from the answer in advance. In the
[1, 1, 1, 1, 3, 5]
example, we do taken = 3
and ans -= 3
in advance, and later we do ans += 2; ans += 4; ans += 6
. This optimization is also used in Approach 2.
public int minIncrementForUnique(int[] A) {
int[] count = new int[100000];
for (int x : A)
count[x]++;
int ans = 0, taken = 0;
for (int x = 0; x < 100000; ++x) {
if (count[x] >= 2) {
taken += count[x] - 1;
ans -= x * (count[x] - 1);
} else if (taken > 0 && count[x] == 0) {
taken--;
ans += x;
}
}
return ans;
}