mirror of
https://github.com/lWolvesl/leetcode.git
synced 2026-02-27 03:38:08 +08:00
routine
This commit is contained in:
21
26/01/go/1984.go
Normal file
21
26/01/go/1984.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user