Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit cea9723

Browse files
committed
945 accepted. 72ms faster than 96.55%
1 parent 7ca3dfc commit cea9723

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

Algorithms/0945.minimum-increment-to-make-array-unique/minimum-increment-to-make-array-unique.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package problem0945
22

3-
func minIncrementForUnique(A []int) int {
4-
size := len(A)
5-
if size == 0 {
6-
return 0
7-
}
3+
import "sort"
84

9-
rec := [80000]bool{}
5+
func minIncrementForUnique(A []int) int {
106

11-
rec[A[0]] = true
7+
sort.Ints(A)
128

9+
size := len(A)
1310
res := 0
14-
1511
for i := 1; i < size; i++ {
16-
n := A[i]
17-
for rec[n] {
18-
n++
19-
res++
12+
d := A[i-1] - A[i] + 1
13+
if d > 0 {
14+
res += d
15+
A[i] += d
2016
}
21-
rec[n] = true
2217
}
2318

2419
return res

0 commit comments

Comments
 (0)