Skip to content

Commit f5a1882

Browse files
committed
adding algo
1 parent d09f855 commit f5a1882

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def insert(self,
6+
intervals: List[List[int]],
7+
newInterval: List[int]) -> List[List[int]]:
8+
9+
intervals.append(newInterval)
10+
11+
answer = []
12+
13+
for i in sorted(intervals, key=lambda x: x[0]):
14+
15+
if answer and i[0] <= answer[-1][-1]:
16+
answer[-1][-1] = max(i[-1],answer[-1][-1])
17+
else:
18+
answer.append(i)
19+
20+
return answer
21+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_48_insert_interval import Solution
4+
5+
class InsertIntervalsTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.insert(intervals = [[1,3],[6,9]], newInterval = [2,5])
10+
target = [[1,5],[6,9]]
11+
self.assertEqual(output, target)
12+
13+
def test_second_pattern(self):
14+
solution = Solution()
15+
output = solution.insert(intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8])
16+
target = [[1,2],[3,10],[12,16]]
17+
self.assertEqual(output, target)
18+

0 commit comments

Comments
 (0)