Leetcode Note: Go - Minimum Difference Between Highest and Lowest of K Scores

Minimum Difference Between Highest and Lowest of K Scores - LeetCode
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/

  • Go 言語で取り組んだメモ

回答

sort then use slide window - Minimum Difference Between Highest and Lowest of K Scores - LeetCode
https://leetcode.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/solutions/1443174/sort-then-use-slide-window/

func minimumDifference(nums []int, k int) int {
    if k == 1 {
        return 0
    }
    sort.Ints(nums)
    m := 100000
    for i := k - 1; i < len(nums); i++ {
        if m > nums[i] - nums[i-k+1] {
            m = nums[i] - nums[i-k+1]
        }
    }
    return m
}