【Problem Description】
给定整数数组 A,每次 move 操作将会选择任意 A[i],并将其递增 1。
返回使 A 中的每个值都是唯一的最少操作次数。
示例 1:
输入:[1,2,2]
输出:1
解释:经过一次 move 操作,数组将变为 [1, 2, 3]。
示例 2:
输入:[3,2,1,2,1,7]
输出:6
解释:经过 6 次 move 操作,数组将变为 [3, 4, 1, 2, 5, 7]。
可以看出 5 次或 5 次以下的 move 操作是不能让数组的每个值唯一的。
提示:
0 <= A.length <= 40000
0 <= A[i] < 40000
[Thinking] answer
1. Sort O (NlogN)
To sort, and then sequentially through the array elements, if the current element is less than equal to its previous element, which then becomes the previous number +1.
class Solution {
public int minIncrementForUnique(int[] A) {
// 先排序
Arrays.sort(A);
int move = 0;
// 遍历数组,若当前元素小于等于它的前一个元素,则将其变为前一个数+1
for (int i = 1; i < A.length; i++) {
if (A[i] <= A[i - 1]) {
int pre = A[i];
A[i] = A[i - 1] + 1;
move += A[i] - pre;
}
}
return move;
}
}
2. Note that the last count derivation sorting O (N)
class Solution {
public int minIncrementForUnique(int[] A) {
// counter数组统计每个数字的个数。
//(这里为了防止下面遍历counter的时候每次都走到40000,所以设置了一个max,这个数据量不设也行,再额外设置min也行)
int[] counter = new int[40001];
int max = -1;
for (int num: A) {
counter[num]++;
max = Math.max(max, num);
}
// 遍历counter数组,若当前数字的个数cnt大于1个,则只留下1个,其他的cnt-1个后移
int move = 0;
for (int num = 0; num <= max; num++) {
if (counter[num] > 1) {
int d = counter[num] - 1;
move += d;
counter[num + 1] += d;
}
}
// 最后, counter[max+1]里可能会有从counter[max]后移过来的,counter[max+1]里只留下1个,其它的d个后移。
// 设 max+1 = x,那么后面的d个数就是[x+1,x+2,x+3,...,x+d],
// 因此操作次数是[1,2,3,...,d],用求和公式求和。
int d = counter[max + 1] - 1;
move += (1 + d) * d / 2;
return move;
}
}
3. The linear probe method (with path compression) O (N) Solution immortal
- The original array is mapped to an address area does not conflict, and resolve conflicts linear probing hash comparison method similar
- Direct linear probe might lead to repeated detection due to a conflict take too long - a route during compression> Consider the detection of
- After a path is detected after a final empty position x, the value of these paths have become subscript x empty location, then if the next detection point is a point on this path, you can directly Jump to empty the detected position x, x from the beginning to continue the probe.
Sample 2 with the following: [3, 2, 1, 2, 1, 7], to simulate the process over linear detection.
Int move by process simulation to the number of records (i.e., the number of increments required) operation.
step1: Insert 3:
Because the position 3 is empty, so you can directly into the 3. (At this array into a figure, red this time of change)
move = 0 remains unchanged;
step2: Insert 2:
Because the position 2 is empty, so you can directly into the 2. (At this array into a figure, red this time of change)
move = 0 remains unchanged;
Step3: 1 insert:
Because position 1 is empty, so you can directly into the 1. (At this array into a figure, red this time of change)
move = 0 remains unchanged;
Step4: Insert 2:
At this point we have found 2 position value, so continue probing backwards until you find the space 4, then 2 to 4 maps.
⚠️ and! ! We want to just walk the path of 2-> 3-> 4 compression, ie their values are set based sub detected vacancy 4 (the next time will be from 4 to detect directly back to find a ~ ~).
(At this array into a figure, red this time of change)
move = move + 4 - 2 = 2;
STEP5: 1 insert:
At this point we find a location already has a value 1, then the probe back to the probe 2, has found that the value at position 2, but since the last saved space 2 in the last 4 process, we jump directly go 4 + 1 i.e. the probe from the start on the line 5 (without the need to take repeated again 2-> 3-> 4 this path myself!), we found that this time slot is 5, so the maps 1 to 5, and for just path traversed 1-> 2-> 5 even if the compression path are mapped to 5!
(At this array into a figure, red this time of change)
move = move + 5 - 1 = 6;
Step6: Insert 7:
7 because the location is empty, so you can directly into the 7. (At this array into a figure, red this time of change)
move = 6 remains unchanged;
Above, the final move to 6.
class Solution {
int[] pos = new int [80000];
public int minIncrementForUnique(int[] A) {
Arrays.fill(pos, -1); // -1表示空位
int move = 0;
// 遍历每个数字a对其寻地址得到位置b, b比a的增量就是操作数。
for (int a: A) {
int b = findPos(a);
move += b - a;
}
return move;
}
// 线性探测寻址(含路径压缩)
private int findPos(int a) {
int b = pos[a];
// 如果a对应的位置pos[a]是空位,直接放入即可。
if (b == -1) {
pos[a] = a;
return a;
}
// 否则向后寻址
// 因为pos[a]中标记了上次寻址得到的空位,因此从pos[a]+1开始寻址就行了(不需要从a+1开始)。
b = findPos(b + 1);
//递归
pos[a] = b; // ⚠️寻址后的新空位要重新赋值给pos[a]哦,路径压缩就是体现在这里。
return b;
}
}
4. greedy algorithm time complexity: O (Nlog N) space complexity: O (1)
public int minIncrementForUnique(int[] A) {
int len = A.length;
if (len == 0) {
return 0;
}
Arrays.sort(A);
// 打开调试
// System.out.println(Arrays.toString(A));
int preNum = A[0];
int res = 0;
for (int i = 1; i < len; i++) {
// preNum + 1 表示当前数「最好」是这个值
if (A[i] == preNum + 1) {
preNum = A[i];
} else if (A[i] > preNum + 1) {
// 当前这个数已经足够大,这种情况可以合并到上一个分支
preNum = A[i];
} else {
// A[i] < preNum + 1
res += (preNum + 1 - A[i]);
preNum++;
}
}
return res;
}
【to sum up】
- Thinking too restrictive, the same statistics as +1, respectively, using a two-cycle results in a timeout.
- Thinking not jump, do not grasp the whole, the second method and the third method looked a long time.
Reprinted from: https://leetcode-cn.com/problems/minimum-increment-to-make-array-unique/solution/ji-shu-onxian-xing-tan-ce-fa-onpai-xu-onlogn-yi -ya /