Non-overlapping Intervals
Real-world version: Drop the Clashing Kiln RunsThe statement here is our own: the same inputs, outputs and technique.
A ceramics studio books firings on one shared kiln. Every request comes in as a pair [start, finish] inside runs, both measured in minutes from the studio's reference mark; a request that sits earlier than the mark uses negative minutes, and a firing always finishes after it starts.
Two requests clash when the stretches of kiln time they occupy share at least one minute. A firing that finishes on the exact minute another begins does not clash with it, since the kiln is free again at that mark.
Cancel as few requests as possible so that no two survivors clash. Return how many requests you had to cancel.
Examples
Example 1
- Input
- runs = [[1, 4], [2, 6], [5, 9]]
- Output
- 1
Cancelling the request that runs from 2 to 6 leaves the other two, which share no minute. Keeping all three is impossible because the middle firing clashes with both of the others.
Example 2
- Input
- runs = [[-3, 0], [0, 5], [5, 8]]
- Output
- 0
Each firing finishes exactly when the next one begins, so the kiln is never double booked and nothing has to go.
Example 3
- Input
- runs = [[0, 10], [9, 12], [11, 20]]
- Output
- 1
Dropping the short middle request keeps the two long firings, which never overlap. Cancelling one of the long firings instead would leave the remaining pair still clashing.
Constraints
- 1 <= runs.length <= 10^5
- runs[i].length == 2
- -5 * 10^4 <= start < finish <= 5 * 10^4
- Requests that only touch at a single minute mark do not clash
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 min_runs_to_cancel(runs: list[list[int]]) -> int:public int minRunsToCancel(int[][] runs)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 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
- Minimize the Maximum Difference of PairsReal-world version: Pairing Gears by Closest Tooth CountMedium
- Path Existence Queries in a Graph IIReal-world version: Relay Hops Across The Field NetworkHard
- Reducing DishesReal-world version: Running Order For The Demo ReelHard
- Assign CookiesReal-world version: Scratch Space HandoutEasy