-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path56.py
55 lines (40 loc) · 1.44 KB
/
56.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from typing import List
import unittest
# Name: Merge Intervals
# Link: https://leetcode.com/problems/merge-intervals/
# Method: Sort and pairwise compare
# Time: O(n*log(n))
# Space: O(n) Result array
# Difficulty: Medium
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# Sort intervals
sorted_interv = list(sorted(intervals, key=lambda x: x[0]))
# For each conseq pair, see if merge-able, if not, put prev in ans arr
span_now = sorted_interv[0]
ans = []
for i in range(1, len(intervals)):
start, end = sorted_interv[i]
if start <= span_now[1]:
span_now[1] = max(span_now[1], end)
else:
ans.append(span_now)
span_now = sorted_interv[i]
ans.append(span_now)
return ans
class TestSolution(unittest.TestCase):
def setUp(self):
self.sol = Solution()
def test_one_interval(self):
res = self.sol.merge([[1, 4]])
self.assertEqual(res, [[1, 4]])
def test_exact_overlap(self):
res = self.sol.merge([[1, 4], [4, 10], [10, 20]])
self.assertEqual(res, [[1, 20]])
def test_lc1(self):
res = self.sol.merge([[1, 3], [2, 6], [8, 10], [15, 18]])
self.assertEqual(res, [[1, 6], [8, 10], [15, 18]])
def test_lc2(self):
res = self.sol.merge([[1, 4], [4, 5]])
self.assertEqual(res, [[1, 5]])
unittest.main()