This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Sort by start, then each interval only has to look at the last one kept.
Builds on Heaps & Top K.
The question to ask
If I sort these by start (or end), does each one only need to compare with the previous?
Interval questions get easy once the intervals are sorted, by start for merging and inserting and by end for picking the most non-overlapping ones. After sorting, each interval only has to compare itself with the last one kept. Two intervals [a, b] and [c, d] overlap when a < d and c < b, and merging takes the min start and max end. For "how many at once" questions, split each interval into a +1 at its start and a -1 at its end and sweep.
Time O(n log n) for the sort · Space O(n) for the output
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
To remove the fewest intervals, keep the one that ends earliest whenever two clash, because it leaves the most room for what follows. Sort by end and count the clashes.
function eraseOverlapIntervals(intervals: number[][]): number {
const sorted = [...intervals].sort((a, b) => a[1] - b[1]);
let removed = 0;
let lastEnd = Number.NEGATIVE_INFINITY;
for (const [start, end] of sorted) {
if (start < lastEnd) removed++;
else lastEnd = end;
}
return removed;
}Car Pooling: each trip adds passengers at its start and removes them at its end. Sort the events (drop-offs before pickups at the same spot) and track the running total.
function carPooling(trips: number[][], capacity: number): boolean {
const events: [number, number][] = [];
for (const [passengers, from, to] of trips) {
events.push([from, passengers], [to, -passengers]);
}
events.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
let load = 0;
for (const [, change] of events) {
load += change;
if (load > capacity) return false;
}
return true;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given a sorted unique integer array, return the smallest list of ranges that cover all the numbers exactly, like '0->2'.
"Ranges" over sorted values: extend the current range while the next number is consecutive, otherwise close it and start another.
Not attempted yet
Merge all overlapping intervals and return the non-overlapping intervals that cover the input.
"Merge" "overlapping" "intervals": sort by start, extend the last kept or start a new one.
Not attempted yet
Insert a new interval into a sorted list of non-overlapping intervals, merging where necessary.
Already sorted, so "insert" is three phases: copy intervals ending before it, merge every overlapping one into it, copy the rest. O(n), no sort.
Not attempted yet
Return the minimum number of intervals to remove so the rest are non-overlapping.
Fewest removals is the same as most "non-overlapping" kept: greedy by earliest end.
Not attempted yet
Balloons span horizontal ranges with a start and end. An arrow shot vertically bursts every balloon it touches. Return the minimum arrows needed.
"Balloons" as ranges: sort by end, shoot at the first end, skip every balloon that covers it.
Not attempted yet
Given two lists of disjoint, sorted intervals, return the intersection of the two lists.
Two sorted "intervals" lists: one pointer each, record max(starts) to min(ends) if valid, advance whichever ends first.
Not attempted yet
A car with a given capacity drives east. Trips pick up passengers at one point and drop them at another. Can it complete every trip?
"Passengers" on board over distance: a sweep line of +pickups and -drop-offs. A difference array works too, since positions are small.
Not attempted yet
Implement a calendar that adds a booking [start, end) only if it causes no double booking with an existing event.
"Booking" without double booking: check the new interval against the neighbours in sorted order (a list, or a balanced tree at scale).
Not attempted yet
For each query point, return the size of the smallest interval that contains it, or -1.
Offline queries over "intervals": sort both, push intervals that start before the query into a min-heap by size, pop the ones that end before it.
Not attempted yet