import heapq
class Solution:
"""
@param intervals: the given k sorted interval lists
@return: the new sorted interval list
"""
def mergeKSortedIntervalLists(self, intervals):
# write your code here
minheap = []
for i in xrange(len(intervals)):
if intervals[i]:
heapq.heappush(minheap, (intervals[i][0].start, intervals[i][0].end, i, 0))
if not minheap:
return []
result = []
lo, hi = minheap[0][0], minheap[0][1]
while minheap:
start, end, i, j = heapq.heappop(minheap)
# push in next element if available
if j + 1 < len(intervals[i]):
heapq.heappush(minheap, (intervals[i][j+1].start, intervals[i][j+1].end, i, j+1))
# compare start, end with the current interval
if start > hi:
result.append(Interval(lo, hi))
lo, hi = start, end
else:
hi = max(hi, end)
result.append(Interval(lo, hi))
return result