Check if All the Integers in a Range Are Covered
The goal is to check whether every integer between left
and right
(both inclusive) is covered by at least one range in ranges
. Here’s a solution for this problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Solution:
def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
# Iterate through each integer from left to right
for i in range(left, right + 1):
# Initialize a variable to track if the current integer is covered
is_covered = False
# Iterate through each range in ranges
for r in ranges:
start, end = r[0], r[1]
# Check if the current integer is within the range
if start <= i and i <= end:
is_covered = True
break # Break the loop as we found a covering range
# If the current integer is not covered by any range, return False
if not is_covered:
return False
# If every integer is covered, return True
return True
|
This code first loops through each integer from left
to right
. Then, for each integer, it checks if it is covered by any range in ranges
. If any integer is not covered, the function returns False
. If all integers are covered, it returns True
.