Grokking LinkedIn Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
Clean solution

Mohammed Dh Abbas

Jul 12, 2024

#class Interval: # def __init__(self, start, end): # self.start = start # self.end = end # def print_interval(self): # print("[" + str(self.start) + ", " + str(self.end) + "]", end='') class Solution: def merge(self, intervals): result = [] # sort the intervals in-order to merge them intervals.sort(key = lambda i: i.start) for interval in intervals: if not result: result.append(interval) else: # [a, b] is the previous interval in the result array # [c, d] is the current internal from the loop a, b = result[-1].start, result[-1].end c, d = interval.start, interval.end # if overlap if c <= b: # merge then update the result array merged = Interval(a, max(b, d)) result[-1] = merged else: # just add the new interval result.append(interval) return result

0

0

Comments
Comments

On this page