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 1:
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.
Example 2:
Input: ranges = [[1,10],[10,20]], left = 21, right = 21 Output: false Explanation: 21 is not covered by any range.
Constraints:
1 <= ranges.length <= 501 <= starti <= endi <= 501 <= left <= right <= 50When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy is like using a checklist. We go through every single number we're interested in, from the start to the end of the target range, and for each one, we exhaustively search through all the provided number ranges to see if it's covered.
Here's how the algorithm would work step-by-step:
def is_covered_brute_force(ranges, left, right):
# We must check every integer from left to right to ensure full coverage of the target range.
for number_to_check in range(left, right + 1):
is_covered_by_any_interval = False
# This flag tracks if the current number is found; it must be reset for each new number.
for current_interval in ranges:
if current_interval[0] <= number_to_check <= current_interval[1]:
is_covered_by_any_interval = True
break
# If after checking all intervals the number is not covered, the whole range fails immediately.
if not is_covered_by_any_interval:
return False
# If the outer loop completes, every number was covered, so the entire range is covered.
return TrueThe core idea is to simplify the problem by first combining any overlapping number ranges into larger, continuous segments. Once we have this cleaned-up list of covered segments, we can easily check if we can 'walk' from our required start number to our end number by jumping between these segments without falling into any gaps.
Here's how the algorithm would work step-by-step:
def check_if_range_is_covered(list_of_intervals, range_start, range_end):
# Sorting allows us to process intervals greedily based on their start points.
list_of_intervals.sort()
furthest_point_covered = range_start - 1
interval_list_index = 0
while furthest_point_covered < range_end:
max_reach_in_current_step = furthest_point_covered
# From all reachable intervals, find the one that extends our coverage the farthest.
while interval_list_index < len(list_of_intervals) and list_of_intervals[interval_list_index][0] <= furthest_point_covered + 1:
max_reach_in_current_step = max(max_reach_in_current_step, list_of_intervals[interval_list_index][1])
interval_list_index += 1
# If we cannot expand our covered zone, it indicates a gap, so the range isn't fully covered.
if max_reach_in_current_step == furthest_point_covered:
return False
furthest_point_covered = max_reach_in_current_step
return True| Case | How to Handle |
|---|---|
| The target range is a single point where left equals right. | The solution must correctly verify if this single integer is covered by any interval. |
| The starting integer 'left' is not covered by any interval in ranges. | Any correct approach will fail to find a cover for 'left' and must return false. |
| A gap exists where a number between left and right is not covered, even if left and right themselves are. | The solution must check every integer in the target range to correctly identify such uncovered gaps. |
| The provided intervals in 'ranges' are unsorted. | A solution should either not rely on sorted order or it must explicitly sort the intervals as a preprocessing step. |
| Multiple intervals in 'ranges' are heavily overlapping, covering the same numbers. | The logic correctly handles this redundancy by only requiring a number to be covered by at least one interval. |
| The target range extends beyond the union of all given intervals. | The check for a number greater than the maximum reach of all intervals will fail, resulting in a false return. |
| The entire target range [left, right] is contained within a single, larger interval from 'ranges'. | This is a simple true case that should be handled correctly by any valid algorithm. |
| Inputs use the extreme boundary values allowed by the constraints, such as 1 and 50. | The solution must use inclusive checks (<=) to correctly handle numbers on the boundaries of intervals and the target range. |