Shared Telescope Night
Astronomers request time on an observatory's one big telescope. Each request is a window of minutes from midnight: [-90, -30] is 10:30 pm to 11:30 pm, and [30, 90] is 12:30 am to 1:30 am.
Only one astronomer can use it at a time, though a session may start the minute another ends. Too many requests came in tonight, and the scheduler wants to turn down as few as possible.
Your task
Write fewestToDecline(requests). Each request is [start, end] with start < end. Return the smallest number of requests to remove so that no two remaining requests overlap.
Two requests overlap if one starts before the other ends. Requests that only touch, like [0, 60] and [60, 120], do not overlap. An empty list returns 0.
fewestToDecline(requests: int[][]) → int
Examples
requests = [[0, 180], [30, 60], [60, 120], [150, 240]]
1
Declining [0, 180] is enough, because the other three then fit one after another: 30 to 60, 60 to 120, and 150 to 240. Declining nothing is impossible because [0, 180] overlaps all three of the others.
requests = [[-60, 30], [-90, -30], [30, 90]]
1
[-90, -30] and [-60, 30] overlap between -60 and -30, so one of them must be declined. Either choice works, because [30, 90] fits after both, since a session may start the minute another ends. The answer is 1.
Constraints
- 0 ≤ requests.length ≤ 10,000
- -1,000,000,000 ≤ start < end ≤ 1,000,000,000
Start with brute force
Try every subset, check it has no overlaps, and keep the largest. The answer is the total minus its size.
There are 2n subsets: about a trillion for 40 requests, and the limit here is 10,000. You need a rule that picks requests without trying combinations.
Hints
0 of 4 shown- Hint 1
Declining the fewest is the same as keeping the most. Which request would you keep first?
- Hint 2
Test candidate rules on the first example. Picking the earliest start chooses [0, 180] and blocks everything else, and picking the shortest request can also fail on other inputs. What about the request that ends earliest?
- Hint 3
The earliest-ending request frees the telescope soonest. Any best schedule can swap its first session for this one without losing anything.
- Hint 4
Sort the requests by end time and track a value
freeAtfor when the telescope is next free. For each request in order, if its start is at leastfreeAt, keep it and setfreeAtto its end; otherwise count it as declined.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Always keep the request that ends earliest among those that still fit. Finishing early never hurts: whatever a best schedule keeps first, swapping it for the earliest-ending request frees the telescope at least as soon, so the rest of the schedule is still valid.
Trace the first example. Sorted by end time, the requests are [30, 60], [60, 120], [0, 180], [150, 240].
[30, 60]: the telescope is free, so it is kept, and the telescope is next free at 60.[60, 120]: it starts at 60, which is not before 60, so it is kept because touching requests are allowed. The telescope is next free at 120.[0, 180]: it starts at 0, before 120, so it overlaps a kept request and is declined.[150, 240]: it starts at 150, which is after 120, so it is kept. The telescope is next free at 240.
Three of the four requests are kept, so one is declined. Sorting by start time instead would keep [0, 180] first and decline the other three.
function fewestToDecline(requests) {
const byEnd = [...requests].sort((a, b) => a[1] - b[1]);
let kept = 0;
let freeAt = -Infinity; // when the telescope is next free
for (const [start, end] of byEnd) {
if (start >= freeAt) { // fits after the last kept session
kept++;
freeAt = end;
}
}
return requests.length - kept;
}def fewest_to_decline(requests):
kept = 0
free_at = float('-inf') # when the telescope is next free
for start, end in sorted(requests, key=lambda r: r[1]):
if start >= free_at: # fits after the last kept session
kept += 1
free_at = end
return len(requests) - keptRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.