mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-02-27 11:58:39 +08:00
22 lines
282 B
Go
22 lines
282 B
Go
package A
|
|
|
|
import (
|
|
"sort"
|
|
)
|
|
|
|
func minimumDifference(nums []int, k int) int {
|
|
if k == 1 {
|
|
return 0
|
|
}
|
|
n := len(nums)
|
|
sort.Ints(nums)
|
|
ans := nums[n-1] - nums[0]
|
|
for i := 0; i+k-1 < n; i++ {
|
|
temp := nums[i+k-1] - nums[i]
|
|
if ans > temp {
|
|
ans = temp
|
|
}
|
|
}
|
|
return ans
|
|
}
|