#1893

Check if All the Integers in a Range Are Covered

Easy
ArrayHash TablePrefix SumHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Instead of checking each integer individually, we can create a boolean array to mark covered integers in the range. This allows us to efficiently track coverage in a single pass.

⚙️

Algorithm

4 steps
  1. 1Step 1: Create a boolean array of size 51 (to cover integers from 1 to 50).
  2. 2Step 2: For each range in ranges, mark the corresponding indices in the boolean array as true.
  3. 3Step 3: Check each integer from left to right and see if it is marked as true in the boolean array. If any integer is false, return false.
  4. 4Step 4: If all integers are true, return true.
solution.py9 lines
1def isCovered(ranges, left, right):
2    covered = [False] * 51
3    for start, end in ranges:
4        for i in range(start, end + 1):
5            covered[i] = True
6    for x in range(left, right + 1):
7        if not covered[x]:
8            return False
9    return True

Complexity note: The time complexity is O(n) because we iterate through the ranges and then through the boolean array, which is efficient given the constraints.

  • 1Using a boolean array allows for efficient tracking of coverage.
  • 2Iterating through ranges can be done in a single pass to mark coverage.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.