Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class Solution:
def insert(self,
intervals: List[List[int]],
newInterval: List[int]) -> List[List[int]]:

intervals.append(newInterval)

answer = []

for i in sorted(intervals, key=lambda x: x[0]):

if answer and i[0] <= answer[-1][-1]:
answer[-1][-1] = max(i[-1],answer[-1][-1])
else:
answer.append(i)

return answer

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function merge(nums1: number[], m: number, nums2: number[], n: number): number[] {
// Replace the tail of nums1 with nums2
for (let i = 0; i < n; i++) {
nums1[m + i] = nums2[i];
}
nums1.sort((a, b) => a - b);
return nums1;
}

console.log(merge([1,2,3,0,0,0], 3, [2,5,6], 3))
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from src.my_project.interviews.top_150_questions_round_22\
.ex_48_insert_interval import Solution

class InsertIntervalsTestCase(unittest.TestCase):

def test_first_pattern(self):
solution = Solution()
output = solution.insert(intervals = [[1,3],[6,9]], newInterval = [2,5])
target = [[1,5],[6,9]]
self.assertEqual(output, target)

def test_second_pattern(self):
solution = Solution()
output = solution.insert(intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8])
target = [[1,2],[3,10],[12,16]]
self.assertEqual(output, target)