# 56. Merge Intervals 

'''
Given a collection of intervals, merge all overlapping intervals.

For example,
 Given [1,3],[2,6],[8,10],[15,18],
 return [1,6],[8,10],[15,18]. 
'''

#Array, Sort 



# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """
        
        intervals=sorted(intervals,key=lambda x:x.start)
        
        result=[]
        for interval in intervals:
            if len(result)==0 or interval.start>result[-1].end:
                result.append(interval)
            else:
                result[-1].end=max(result[-1].end,interval.end)
        return result
        