Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luojiyin1987/8c4a3563183d584e8195209d25a5b99e to your computer and use it in GitHub Desktop.
Save luojiyin1987/8c4a3563183d584e8195209d25a5b99e to your computer and use it in GitHub Desktop.
func minMoves(nums []int) int {
sort.Ints(nums)
min := nums[0]
sum :=0
for _, v := range nums {
sum +=v
}
return sum - min * len(nums)
}
func minMoves(nums []int) int {
min := math.MaxInt32
for _, n := range nums {
if n < min {
min = n
}
}
res := 0
for _, n := range nums {
res += n-min
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment