go: add «945. Minimum Increment to Make Array Unique»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
d738179503
commit
d18212e90f
1 changed files with 17 additions and 0 deletions
17
go/minimum-increment-to-make-array-unique.go
Normal file
17
go/minimum-increment-to-make-array-unique.go
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
package minimum_increment_to_make_array_unique
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
|
func minIncrementForUnique(nums []int) int {
|
||||||
|
slices.Sort(nums)
|
||||||
|
|
||||||
|
moves := 0
|
||||||
|
for i := 1; i < len(nums); i++ {
|
||||||
|
if nums[i] <= nums[i-1] {
|
||||||
|
moves += nums[i-1] + 1 - nums[i]
|
||||||
|
nums[i] = nums[i-1] + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return moves
|
||||||
|
}
|
Loading…
Reference in a new issue