βœ…Merge Intervals

Logic
  1. For every interval in intervals

    1. If it comes before the newInterval with no overlap: current.end < newInterval.start

      1. Push current into the result vector

    2. If newInterval comes before the current interval with no overlap: current.start > newInterval.end

      1. Push newInterval into the result vector

      2. current is the new newInterval

    3. If both conditions are false then definitely there has been an overlap

      1. Merge the current interval to newInterval

  2. In the end, newInterval will be left

    1. Push it into the result vector

Code
Compleixty

Time Complexity: O(n)O(n)​

Space Complexity: O(n)O(n)​

Last updated