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
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!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code