-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge_intervals.py
39 lines (32 loc) · 1.32 KB
/
merge_intervals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from typing import List
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
"""
Given a collection of intervals, merge all overlapping intervals.
"""
if not intervals:
return []
intervals.sort(key=lambda x: x[0])
merged = [intervals[0]]
for i in range(1, len(intervals)):
if intervals[i][0] <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], intervals[i][1])
else:
merged.append(intervals[i])
return merged
# Time complexity: O(n log n)
# Space complexity: O(n)
import unittest
class TestSolution(unittest.TestCase):
def setUp(self):
self.solution = Solution()
def test_merge(self):
self.assertEqual(self.solution.merge([[1,3],[2,6],[8,10],[15,18]]), [[1,6],[8,10],[15,18]])
self.assertEqual(self.solution.merge([[1,4],[4,5]]), [[1,5]])
self.assertEqual(self.solution.merge([[1,4],[0,4]]), [[0,4]])
self.assertEqual(self.solution.merge([[1,4],[2,3]]), [[1,4]])
self.assertEqual(self.solution.merge([[1,4],[0,0]]), [[0,0],[1,4]])
self.assertEqual(self.solution.merge([[1,4],[0,1]]), [[0,4]])
self.assertEqual(self.solution.merge([[1,4],[0,2],[3,5]]), [[0,5]])
if __name__ == '__main__':
unittest.main()