Check if All the Integers in a Range Are Covered
Real-world version: Patrol Coverage AuditThe statement here is our own: the same inputs, outputs and technique.
A depot numbers its patrol posts with consecutive integers. The night rota is given as shifts, where shifts[i] = [from_i, to_i] means guard i walks every post numbered from_i through to_i. Both ends of a walk are included, so a guard with [4, 4] walks post 4 and nothing else.
An auditor picks the span of posts numbered first through last, again including both ends, and wants to know whether the rota leaves no hole: every post in that span must be walked by at least one guard. Different guards may cover different parts of the span, and walks may overlap or sit side by side.
Return true if every post from first to last is walked by at least one guard, and false otherwise.
Examples
Example 1
- Input
- shifts = [[2, 5], [6, 9]], first = 3, last = 8
- Output
- true
The audited posts are 3, 4, 5, 6, 7 and 8. The first guard walks 3, 4 and 5, and the second walks 6, 7 and 8, so none of the six posts is left without a guard.
Example 2
- Input
- shifts = [[2, 5], [7, 9]], first = 4, last = 8
- Output
- false
Post 6 lies inside the audited span, but the first guard stops at post 5 and the second starts at post 7, so post 6 is walked by nobody.
Example 3
- Input
- shifts = [[7, 7]], first = 8, last = 8
- Output
- false
The audited span is the single post 8, and the only guard on the rota walks post 7 alone.
Constraints
- 1 <= shifts.length <= 50
- shifts[i].length == 2
- 1 <= shifts[i][j] <= 50
- shifts[i][0] <= shifts[i][1]
- 1 <= first <= last <= 50
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def posts_all_covered(shifts: list[list[int]], first: int, last: int) -> bool:public boolean postsAllCovered(int[][] shifts, int first, int last)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 20 test cases: 20 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Subarray Sum Equals KReal-world version: Trail Elevation StretchesMedium
- Contiguous ArrayReal-world version: Balanced Shift StretchMedium
- Count Number of Nice SubarraysReal-world version: Runs With K Odd ReadingsMedium
- Count Subarrays With Majority Element IReal-world version: Stretches Carried by One CandidateMedium