Skip to content

Latest commit

 

History

History
41 lines (34 loc) · 1.31 KB

1893. Check if All the Integers in a Range Are Covered.md

File metadata and controls

41 lines (34 loc) · 1.31 KB

问题描述

You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.

Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.

An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.

Example:

Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
Output: true
Explanation: Every integer between 2 and 5 is covered:
- 2 is covered by the first range.
- 3 and 4 are covered by the second range.
- 5 is covered by the third range.
Input: ranges = [[1,10],[10,20]], left = 21, right = 21
Output: false
Explanation: 21 is not covered by any range.

思路

暴力

代码

class Solution:
    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
        for num in range(left, right + 1):
            flag = 0
            for s, e in ranges:
                if s <= num <= e:
                    flag = 1
                    break
            if flag == 0: return False
        return True