diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_48_insert_interval.py b/src/my_project/interviews/top_150_questions_round_22/ex_48_insert_interval.py new file mode 100644 index 00000000..c2671276 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_48_insert_interval.py @@ -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 + diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_01_merge_sorted_array.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_01_merge_sorted_array.ts new file mode 100644 index 00000000..2403ffda --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_01_merge_sorted_array.ts @@ -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)) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_48_insert_interval_round_22.py b/tests/test_150_questions_round_22/test_48_insert_interval_round_22.py new file mode 100644 index 00000000..70cdfd19 --- /dev/null +++ b/tests/test_150_questions_round_22/test_48_insert_interval_round_22.py @@ -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) +