Exemple #1
0
 def test_merge_intervals(self):
     interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
     merged_intervals = merge_intervals(interval_list)
     self.assertEqual(
         merged_intervals,
         [[1, 6], [8, 10], [15, 18]]
     )
Exemple #2
0
 def test_merge_intervals(self):
     interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
     merged_intervals = merge_intervals(interval_list)
     self.assertEqual(
         merged_intervals,
         [[1, 6], [8, 10], [15, 18]]
     )
Exemple #3
0
    def print_intervals(intervals):
        """
        Prints out the intervals.
        """
        res = []
        for i in intervals:
            res.append(repr(i))
        print("".join(res))


def merge_intervals(intervals):
    """ Merges intervals in the form of list. """
    if intervals is None:
        return None
    intervals.sort(key=lambda i: i[0])
    out = [intervals.pop(0)]
    for i in intervals:
        if out[-1][-1] >= i[0]:
            out[-1][-1] = max(out[-1][-1], i[-1])
        else:
            out.append(i)
    return out


from algorithms.arrays import merge_intervals

a = [[1, 3], [2, 6], [8, 10], [15, 18]]

print(merge_intervals(a))

#박제준 3/16