In this article, we will be solving LeetCode’s Interval List Intersections in JavaScript. This problem uses the two-pointer approach. The two-pointer approach is usually used to keep track of array or string indices to solve a problem. One variation is one pointer being slow and the other pointer being fast meaning that the fast pointer is ahead of the slow pointer while traversing through the array/string. A classic example of this is to remove duplicates from a sorted array. Another variation is one pointer starts from the beginning while the other pointer starts from the end. The pointers move toward each other until they both meet. In our approach for this problem, we will be using a pointer for each array that start at the beginning and move them separately based on what conditions are met.

Problem

You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, endi] and secondList[j] = [startj, endj]. Each list of intervals is pairwise disjoint and in sorted order.

Return the intersection of these two interval lists.

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 example, the intersection of [1, 3] and [2, 4] is [2, 3].

#javascript

Solving LeetCode’s Interval List Intersections
1.05 GEEK