Greedy Algorithms8 sections · 316 units
Open in Course

Interval Intersections - Algorithm

Two-pointer approach

Use two pointers, one for each list. For current intervals A[i] and B[j]:

1.1. Compute intersection: [max(A[i].start, B[j].start), min(A[i].end, B[j].end)].

2.2. If start <= end, add to result.

3.3. Move the pointer of the interval that ends earlier (it cannot intersect any more intervals in the other list). It works because both lists are sorted. Time: O(n+m)O(n + m). Space: O(k)O(k) for kk intersections.