go: add «1509. Minimum Difference Between Largest and Smallest Value in Three Moves»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-03 09:57:36 +02:00
parent d89dbdcac2
commit bcabf01c65
Signed by: mfocko
SSH key fingerprint: SHA256:5YXD7WbPuK60gxnG6DjAwJiS9+swoWj33/HFu8g8JVo

View file

@ -0,0 +1,18 @@
package main
import "slices"
func minDifference(nums []int) int {
if len(nums) <= 4 {
return 0
}
slices.Sort(nums)
diff := nums[len(nums)-1] - nums[0]
for i := 0; i < 4; i++ {
diff = min(diff, nums[len(nums)-4+i]-nums[i])
}
return diff
}