LeetCode Problem Workspace

Check if All the Integers in a Range Are Covered

Check if all integers in the range [left, right] are covered by intervals in a given set of ranges.

category

3

Topics

code_blocks

6

Code langs

hub

3

Related

Practice Focus

Easy · Array scanning plus hash lookup

bolt

Answer-first summary

Check if all integers in the range [left, right] are covered by intervals in a given set of ranges.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Array scanning plus hash lookup

Try AiBox Copilotarrow_forward

This problem asks whether each integer in a range [left, right] is covered by at least one interval from a list of ranges. A direct solution would involve iterating through every number between left and right, checking for coverage in each interval. Efficient array scanning plus hash lookups can provide a solution.

Problem Statement

You are given a 2D integer array ranges where each element represents an inclusive range [starti, endi]. Additionally, you are given two integers, left and right, which define an inclusive range of integers. Your task is to determine if every integer within this range [left, right] is covered by at least one of the given ranges.

An integer x is covered by a range [starti, endi] if it satisfies starti <= x <= endi. Return true if every integer in [left, right] is covered, and false otherwise. For example, with ranges = [[1, 2], [3, 4], [5, 6]], left = 2, and right = 5, the correct output would be true.

Examples

Example 1

Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5

Output: true

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

21 is not covered by any range.

Constraints

  • 1 <= ranges.length <= 50
  • 1 <= starti <= endi <= 50
  • 1 <= left <= right <= 50

Solution Approach

Array Scanning

Start by iterating over every integer from left to right. For each integer, check if it falls within any of the provided ranges. This brute-force approach is simple but may not be efficient for large ranges.

Hash Table Lookup

Instead of checking each integer individually, you can use a hash table to track the coverage of numbers within the ranges. This can improve performance by quickly checking if a number falls within any range.

Prefix Sum Approach

You can also optimize this by marking covered intervals in a prefix sum array, and then checking whether every index in the range [left, right] is covered by some interval. This method provides a more efficient solution in certain scenarios.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

The time complexity depends on the approach: the brute-force solution has a time complexity of O(n*m), where n is the number of ranges and m is the size of the range [left, right]. Using a hash table or prefix sum approach can reduce the time complexity to O(m + n). The space complexity is O(n) due to the storage requirements for the ranges or hash table.

What Interviewers Usually Probe

  • The candidate shows understanding of array iteration techniques.
  • The candidate demonstrates awareness of optimizing brute-force solutions.
  • The candidate considers the use of advanced data structures for improving performance.

Common Pitfalls or Variants

Common pitfalls

  • Overcomplicating the solution with unnecessary data structures for small inputs.
  • Forgetting to check coverage for every integer between left and right.
  • Confusing inclusive and exclusive range bounds when verifying coverage.

Follow-up variants

  • What if the intervals can overlap?
  • How would you approach the problem if the range size was very large?
  • Can the solution be optimized if ranges are sorted?

FAQ

How can I optimize the brute-force solution for checking range coverage?

You can use hash tables or a prefix sum approach to avoid checking every integer in the range individually, which reduces the time complexity.

What is the main pattern in the 'Check if All the Integers in a Range Are Covered' problem?

The main pattern is array scanning plus hash lookup, which involves checking if each integer in the target range is covered by any given interval.

What are the constraints of the 'Check if All the Integers in a Range Are Covered' problem?

The constraints are that the number of ranges is between 1 and 50, each range has start and end values between 1 and 50, and the range [left, right] is also between 1 and 50.

Can the solution for this problem be optimized further?

Yes, the solution can be optimized by using hash tables or prefix sum arrays to track the coverage efficiently.

What is the time complexity of the brute-force solution for this problem?

The time complexity of the brute-force solution is O(n*m), where n is the number of ranges and m is the size of the range [left, right].

terminal

Solution

Solution 1: Difference Array

We can use the idea of a difference array to create a difference array $\textit{diff}$ of length $52$.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution:
    def isCovered(self, ranges: List[List[int]], left: int, right: int) -> bool:
        diff = [0] * 52
        for l, r in ranges:
            diff[l] += 1
            diff[r + 1] -= 1
        s = 0
        for i, x in enumerate(diff):
            s += x
            if s <= 0 and left <= i <= right:
                return False
        return True
Check if All the Integers in a Range Are Covered Solution: Array scanning plus hash lookup | LeetCode #1893 Easy