bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 986. Interval List Intersections

986. Interval List Intersections

Leetcode Solutions

Merge Intervals Approach

Algorithm

  1. Initialize two pointers i and j to 0 for firstList and secondList respectively.
  2. Initialize an empty list intersections to store the intersections.
  3. While i < len(firstList) and j < len(secondList), do the following: a. Find the maximum of the start values of the current intervals from firstList and secondList. b. Find the minimum of the end values of the current intervals from firstList and secondList. c. If the maximum start value is less than or equal to the minimum end value, there is an intersection, so add the interval [max(start values), min(end values)] to intersections. d. Increment the pointer i or j depending on which interval has the smallest endpoint.
  4. Return the list intersections.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR