Check if All the Integers in a Range Are Covered - Problem

Imagine you're a security guard monitoring a building with multiple surveillance cameras. Each camera covers a specific range of floors, and you need to ensure that every floor in a critical section is under surveillance.

You are given a 2D integer array ranges where each ranges[i] = [starti, endi] represents an inclusive interval of floors covered by camera i. You also have two integers left and right representing the critical section that needs complete coverage.

Your task: Return true if every single floor in the range [left, right] is covered by at least one camera. Return false if any floor is left unmonitored.

A floor x is covered by camera i if starti โ‰ค x โ‰ค endi.

Input & Output

example_1.py โ€” Basic Coverage
$ Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
โ€บ Output: true
๐Ÿ’ก Note: Every integer from 2 to 5 is covered: 2โˆˆ[1,2], 3โˆˆ[3,4], 4โˆˆ[3,4], 5โˆˆ[5,6]
example_2.py โ€” Gap in Coverage
$ Input: ranges = [[1,10],[10,20]], left = 21, right = 21
โ€บ Output: false
๐Ÿ’ก Note: The number 21 is not covered by any range. All ranges end at 20 or earlier.
example_3.py โ€” Single Point Range
$ Input: ranges = [[1,50]], left = 25, right = 25
โ€บ Output: true
๐Ÿ’ก Note: The single number 25 is covered by the range [1,50] since 1 โ‰ค 25 โ‰ค 50

Constraints

  • 1 โ‰ค ranges.length โ‰ค 50
  • 1 โ‰ค starti โ‰ค endi โ‰ค 50
  • 1 โ‰ค left โ‰ค right โ‰ค 50
  • All ranges and target values are small integers

Visualization

Tap to expand
๐Ÿข Building Security Coverage SystemBuilding Floors: 1 2 3 4 5 6 7 8 9 10๐Ÿ“น Cam A[1,2]๐Ÿ“น Cam B[3,4]๐Ÿ“น Cam C[5,6]123456๐ŸŽฏ VIP Section [2, 5]โœ… All VIP floors covered - Security OK!
Understanding the Visualization
1
Deploy Cameras
Each camera covers a range of floors [start, end]
2
Mark Coverage
Create a coverage map showing all monitored floors
3
Check VIP Section
Verify every floor in [left, right] is monitored
4
Security Report
Return true if fully covered, false if any gaps exist
Key Takeaway
๐ŸŽฏ Key Insight: Instead of checking each floor against all cameras repeatedly, create a master coverage map first, then verify the VIP section in one efficient pass!
Asked in
Amazon 42 Google 38 Microsoft 25 Meta 18
36.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen