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

Commit e3636c5

Browse files
committed
945 accepted. 40ms
1 parent cea9723 commit e3636c5

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
package problem0945
22

3-
import "sort"
4-
53
func minIncrementForUnique(A []int) int {
4+
counts := [40001]int{}
5+
max := 0
66

7-
sort.Ints(A)
7+
for _, n := range A {
8+
counts[n]++
9+
if max < n {
10+
max = n
11+
}
12+
}
813

9-
size := len(A)
1014
res := 0
11-
for i := 1; i < size; i++ {
12-
d := A[i-1] - A[i] + 1
13-
if d > 0 {
14-
res += d
15-
A[i] += d
15+
16+
for n := 0; n < max; n++ {
17+
if counts[n] <= 1 {
18+
continue
1619
}
20+
redundance := counts[n] - 1
21+
res += redundance
22+
counts[n+1] += redundance
1723
}
1824

25+
redundance := counts[max] - 1
26+
res += (redundance + 1) * redundance / 2
27+
1928
return res
2029
}

0 commit comments

Comments
 (0)