title: Merging Overlapping Intervals in Python
body: <p>I am trying to solve a question where in overlapping intervals need to be merged.
  <a href="https://leetcode.com/problems/merge-intervals/description/" rel="nofollow noreferrer">The question is</a>:</p>
  <blockquote>
  <p>Given a collection of intervals, merge all overlapping intervals.</p>
  <p>For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].</p>
  </blockquote>
  <p>I tried my solution:</p>
  <pre><code># Definition for an interval.
  # class Interval:
  #     def __init__(self, s=0, e=0):
  #         self.start = s
  #         self.end = e
  
  class Solution:
      def merge(self, intervals):
          &quot;&quot;&quot;
          :type intervals: List[Interval]
          :rtype: List[Interval]
          &quot;&quot;&quot;
          start = sorted([x.start for x in intervals])
          end = sorted([x.end for x in intervals])
          
          merged = []
          j = 0
          new_start = 0
          
          for i in range(len(start)):
              if start[i]&lt;end[j]:
                  continue
              else:
                  j = j + 1
                  merged.append([start[new_start], end[j]])
                  new_start = i
          
          return merged
  </code></pre>
  <p>However it is clearly missing the last interval as:</p>
  <pre><code>Input : [[1,3],[2,6],[8,10],[15,18]]
  
  Answer :[[1,6],[8,10]]
  
  Expected answer: [[1,6],[8,10],[15,18]]
  </code></pre>
  <p>Not sure how to include the last interval as overlap can only be checked in forward mode.</p>
  <p>How to fix my algorithm so that it works till the last slot?</p>