Skip to content

Commit 1991c8b

Browse files
committed
add q945
1 parent 9778651 commit 1991c8b

File tree

3 files changed

+48
-14
lines changed

3 files changed

+48
-14
lines changed

.idea/workspace.xml

Lines changed: 19 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
* [q54_螺旋矩阵](/src/数组操作/q54_螺旋矩阵)
5454
* [q73_矩阵置零](/src/数组操作/q73_矩阵置零)
55+
* [q945_使数组唯一的最小增量](/src/数组操作/q945_使数组唯一的最小增量)
5556

5657
### 栈相关
5758

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package 数组操作.q945_使数组唯一的最小增量;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* 先排序再遍历一次 o(n*log(n))
7+
*/
8+
public class Solution {
9+
10+
public int minIncrementForUnique(int[] A) {
11+
if (A == null || A.length == 0 || A.length == 1) {
12+
return 0;
13+
}
14+
15+
int rs = 0;
16+
Arrays.sort(A);
17+
18+
int t = A[0];
19+
for (int i = 1; i < A.length; i++) {
20+
if (A[i] <= t) {
21+
rs = rs + t - A[i] + 1;
22+
A[i] = t + 1;
23+
}
24+
t = A[i];
25+
}
26+
return rs;
27+
}
28+
}

0 commit comments

Comments
 (0)