Interval List Intersections
MediumP3Coding
Two lists of closed intervals [start, end] are given. Within each list the intervals are disjoint and sorted by start. Return the intersection of the two lists: every non-empty overlap between an interval of the first list and an interval of the second, sorted by start. An overlap of a single point, such as [5,5], counts.
Example 1
- Input
- firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
- Output
- [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]
Example 2
- Input
- firstList = [[1,3],[5,9]], secondList = []
- Output
- []
Constraints
- 0 <= firstList.length, secondList.length <= 1000
- 0 <= start <= end <= 10^9
- Each list is sorted and disjoint (intervals do not even touch).
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.