mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-08-03 02:47:26 +08:00
22 lines
290 B
Go
22 lines
290 B
Go
package leetcode
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
)
|
|
|
|
func minMoves2(nums []int) int {
|
|
if len(nums) == 0 {
|
|
return 0
|
|
}
|
|
moves, mid := 0, len(nums)/2
|
|
sort.Ints(nums)
|
|
for i := range nums {
|
|
if i == mid {
|
|
continue
|
|
}
|
|
moves += int(math.Abs(float64(nums[mid] - nums[i])))
|
|
}
|
|
return moves
|
|
}
|