|
| 1 | +# Interval List Intersections |
| 2 | + |
| 3 | +Given two lists of closed intervals, interval_list_a and interval_list_b, return the intersection of the two interval |
| 4 | +lists. |
| 5 | + |
| 6 | +> A closed interval [start, end] (with start <= end) includes all real numbers x such that start <= x <= end. |
| 7 | +
|
| 8 | +Each interval in the lists has its own start and end time and is represented as [start, end]. Specifically: |
| 9 | + |
| 10 | +- interval_list_a[i] = [starti, endi] |
| 11 | +- interval_list_b[j] = [startj, endj] |
| 12 | + |
| 13 | +The intersection of two closed intervals i and j is either: |
| 14 | +- An empty set, if they do not overlap, or |
| 15 | +- A closed interval [max(starti, startj), min(endi, endj)] if they do overlap. |
| 16 | + |
| 17 | +Also, each list of intervals is pairwise disjoint(intervals within a list don't overlap) and in sorted order. |
| 18 | + |
| 19 | +## Constraints |
| 20 | + |
| 21 | +- 0 <= `interval_list_a.length`, `interval_list_b.length` <= 1000 |
| 22 | +- `interval_list_a.length` + `interval_list_b.length` >= 1 |
| 23 | +- 0 <= `starti` < `endi` <= 10^9 |
| 24 | +- `endi` < `start(i+1)` |
| 25 | +- 0 <= `startj` < `endj` <= 10^9 |
| 26 | +- `endj` < `start(j+1)` |
| 27 | + |
| 28 | +## Examples |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +## Topics |
| 37 | + |
| 38 | +- Two pointers |
| 39 | +- Intervals |
| 40 | + |
| 41 | +## Solutions |
| 42 | + |
| 43 | +1. [Naive Approach](#naive-approach) |
| 44 | +1. [Using Intervals](#optimized-approach-using-intervals) |
| 45 | + |
| 46 | +### Naive Approach |
| 47 | + |
| 48 | +The naive approach for this problem is to use a nested loop for finding intersecting intervals. |
| 49 | + |
| 50 | +The outer loop will iterate for every interval in interval_list_a and the inner loop will search for any intersecting |
| 51 | +interval in the interval_list_b. |
| 52 | + |
| 53 | +If such an interval exists, we add it to the intersections list. |
| 54 | + |
| 55 | +Since we are using nested loops, the time complexity for this naive approach will be O(n*m), where n is the length of |
| 56 | +intervalsA and m is the length of intervalsB. |
| 57 | + |
| 58 | +### Optimized approach using intervals |
| 59 | + |
| 60 | +The essence of this approach is to leverage two key advantages: first, the lists of intervals are sorted, and second, |
| 61 | +the result requires comparing intervals to check for overlap. The algorithm works by iterating through both sorted lists |
| 62 | +of intervals simultaneously, identifying intersections between intervals from the two lists. At each step, it compares |
| 63 | +the current intervals from both lists and determines whether there is an intersection by examining the endpoints of the |
| 64 | +intervals. It adds the intersecting interval to the result list if an intersection exists. To efficiently navigate |
| 65 | +through the intervals, the algorithm adjusts pointers based on the positions of the intervals’ endpoints, ensuring that |
| 66 | +it covers all possible intersections. The algorithm accurately computes the interval list intersection by systematically |
| 67 | +traversing the lists, identifying intersections, and adding them to the results list, the algorithm accurately computes |
| 68 | +the interval list intersection. |
| 69 | + |
| 70 | +The algorithm to solve this problem is as follows: |
| 71 | + |
| 72 | +- We’ll use two indexes, i and j, to iterate through the intervals in both lists, `interval_list_a` and `interval_list_b`, |
| 73 | + respectively. |
| 74 | +- To check whether there’s any intersecting point among the given intervals: |
| 75 | + - Take the starting times of the first pair of intervals from both lists and check which occurs later, storing it in |
| 76 | + a variable, say start. |
| 77 | + - Also, compare the ending times of the same pair of intervals from both lists and store the minimum end time in |
| 78 | + another variable, say, end. |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +- Next, we will check if `interval_list_a[i]` and `interval_list_b[j]` overlap by comparing the start and end times. |
| 84 | + - If the times overlap, then the intersecting time interval will be added to the resultant list, that is, intersections. |
| 85 | + - After the comparison, we need to move forward in one of the two input lists. The decision is taken based on which |
| 86 | + of the two intervals being compared ends earlier. If the interval that ends first is in `interval_list_a`, we move |
| 87 | + forward in that list, else, we move forward in `interval_list_b`. |
| 88 | + |
| 89 | +The illustrations below show the key steps of the solution. |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | +#### Solution summary |
| 99 | + |
| 100 | +Let’s briefly discuss the approach that we have used to solve the above mentioned problem: |
| 101 | + |
| 102 | +- Set two pointers, i and j, at the beginning of both lists, respectively, for their iteration. |
| 103 | +- While iterating, find the latest starting time and the earliest ending time for each pair of intervals |
| 104 | + `interval_list_a[i]` and `interval_list_b[j]`. |
| 105 | +- If the latest starting time is less than or equal to the earliest ending time, store it as an intersection. |
| 106 | +- Increment the pointer (i or j) of the list having the smaller end time of the current interval. |
| 107 | +- Keep iterating until either list is fully traversed. |
| 108 | +- Return the list of intersections. |
| 109 | + |
| 110 | +#### Time Complexity |
| 111 | + |
| 112 | +The time complexity is `O(n+m)`, where n and m are the number of meetings in `interval_list_a` and `interval_list_b`, |
| 113 | +respectively. |
| 114 | + |
| 115 | +#### Space Complexity |
| 116 | + |
| 117 | +The space complexity is `O(1)` as only a fixed amount of memory is consumed by a few temporary variables for computations |
| 118 | +performed by the algorithm. |
0 commit comments