Leetcode Note: Go - Check if All the Integers in a Range Are Covered

Check if All the Integers in a Range Are Covered - LeetCode
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/

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

回答

Go prefix sum - Check if All the Integers in a Range Are Covered - LeetCode
https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/solutions/2098440/go-prefix-sum/

func isCovered(ranges [][]int, left int, right int) bool {
    a := make([]int, 52)
    
    for _, v := range ranges {
        start, end := v[0], v[1] + 1
        
        a[start]++
        a[end]--
    }
    
    for i := 1; i < 51; i++ {
        a[i] += a[i - 1]
    }
    
    for i := left; i <= right; i++ {
        if a[i] < 1 {
            return false
        }
    }
    
    return true
}