Leetcode Note: Go - Smallest Range I

Smallest Range I - LeetCode
https://leetcode.com/problems/smallest-range-i/

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

回答

A golang solution - LeetCode Discuss
https://leetcode.com/problems/smallest-range-i/discuss/293781/A-golang-solution

func smallestRangeI(A []int, K int) int {
    if A == nil || len(A) < 2{
        return 0
    }
    
    smallest := 10001
    biggest := 0
    for i := 0; i < len(A); i++{
        if A[i] < smallest{
            smallest = A[i]
        }
        if A[i] > biggest{
            biggest = A[i]
        }
    }
    if biggest - smallest - 2 * K < 0{
        return 0
    }
    return biggest - smallest - 2 * K
}