Interval List Intersections - Complete Solution Guide
Interval List Intersections is LeetCode problem 986, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given two lists of closed intervals, firstList and secondList , where firstList[i] = [start i , end i ] and secondList[j] = [start j , end j ] . Each list of intervals is pairwise disjoint and in sorted order . Return the intersection of these two interval lists . A closed interval [a, b] (with a <= b ) denotes the set of real numbers x with a <= x <= b . The intersection of two closed intervals is a set of real numbers that are either empty or represented as a closed interval. For examp
Detailed Explanation
The problem asks us to find the intersection of two lists of closed intervals. Both lists are already sorted and contain disjoint intervals. The intersection of two intervals is another interval representing the overlapping region between them. If there is no overlap, the intersection is empty. The input consists of two lists of intervals, `firstList` and `secondList`, where each interval is represented as a pair of integers [start, end]. The output should be a list of intervals representing the intersections of the intervals in the two input lists.
Solution Approach
The solution uses a two-pointer approach. We initialize two pointers, `i` and `j`, to the beginning of `firstList` and `secondList`, respectively. We iterate through both lists, comparing the current intervals. For each pair of intervals, we calculate the intersection. If the intersection is not empty, we add it to the result. We then advance the pointer of the list whose interval ends earlier. This ensures that we don't miss any intersections and that we process the intervals in sorted order.
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `i` and `j`, to 0, representing the current index in `firstList` and `secondList`, respectively.
- Step 2: Initialize an empty list `result` to store the intersection intervals.
- Step 3: While `i` is less than the length of `firstList` and `j` is less than the length of `secondList`:
- Step 4: Get the current intervals from `firstList` and `secondList`: `[start_i, end_i] = firstList[i]` and `[start_j, end_j] = secondList[j]`.
- Step 5: Calculate the potential intersection interval: `low = max(start_i, start_j)` and `high = min(end_i, end_j)`.
- Step 6: If `low <= high`, an intersection exists. Add the interval `[low, high]` to the `result` list.
- Step 7: Compare `end_i` and `end_j`. If `end_i < end_j`, increment `i`. Otherwise, increment `j`.
- Step 8: Return the `result` list.
Key Insights
- Insight 1: The lists are sorted, allowing us to use a two-pointer approach to efficiently iterate through them.
- Insight 2: The intersection of two intervals [a, b] and [c, d] is [max(a, c), min(b, d)] if max(a, c) <= min(b, d).
- Insight 3: We need to advance the pointer of the list whose interval ends earlier. This is because the current interval in that list cannot intersect with any future intervals in the other list, as the intervals are disjoint and sorted.
Complexity Analysis
Time Complexity: O(m+n)
Space Complexity: O(min(m,n))
Topics
This problem involves: Array, Two Pointers, Line Sweep.
Companies
Asked at: DoorDash, Mixpanel, Nuro, Verkada, Yandex.