go: add «945. Minimum Increment to Make Array Unique»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-06-14 09:46:16 +02:00
parent d738179503
commit d18212e90f
Signed by: mfocko
GPG key ID: 7C47D46246790496

View 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
}