diff --git a/Array/57. Insert Interval.py b/Array/57. Insert Interval.py deleted file mode 100644 index 1b0eb5d..0000000 --- a/Array/57. Insert Interval.py +++ /dev/null @@ -1,20 +0,0 @@ -# Array -from typing import List - - -# Time:O(n) -# Space:O(1) -class Solution: - def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: - res = [] - for i in range(len(intervals)): - if newInterval[1] < intervals[i][0]: - res.append(newInterval) - return res + intervals[i:] - elif newInterval[0] > intervals[i][1]: - res.append(intervals[i]) - else: - newInterval = [min(newInterval[0], intervals[i][0]), - max(newInterval[1], intervals[i][1])] - res.append(newInterval) - return res diff --git a/README.md b/README.md index f2bf251..aa6df2d 100644 --- a/README.md +++ b/README.md @@ -41,12 +41,12 @@ Each problem follows this structure: ## Solutions -📊 **Statistics**: 85 problems solved (migrated to new structure) +📊 **Statistics**: 86 problems solved (migrated to new structure) | Difficulty | Count | |------------|-------| | Easy | 60 | -| Medium | 24 | +| Medium | 25 | | Hard | 1 | ### Browse by Difficulty diff --git a/docs/MEDIUM.md b/docs/MEDIUM.md index 108b74f..d85a3ff 100644 --- a/docs/MEDIUM.md +++ b/docs/MEDIUM.md @@ -1,6 +1,6 @@ # Medium Problems -Total: 24 problems solved +Total: 25 problems solved ## Solutions @@ -11,6 +11,7 @@ Total: 24 problems solved | 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [solution.py](../problems/medium/search_in_rotated_sorted_array_33/solution.py) | [test_solution.py](../problems/medium/search_in_rotated_sorted_array_33/test_solution.py) | | 49 | [Group Anagrams](https://leetcode.com/problems/group-anagrams/) | [solution.py](../problems/medium/group_anagrams_49/solution.py) | [test_solution.py](../problems/medium/group_anagrams_49/test_solution.py) | | 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [solution.py](../problems/medium/merge_intervals_56/solution.py) | [test_solution.py](../problems/medium/merge_intervals_56/test_solution.py) | +| 57 | [Insert Interval](https://leetcode.com/problems/insert-interval/) | [solution.py](../problems/medium/insert_interval_57/solution.py) | [test_solution.py](../problems/medium/insert_interval_57/test_solution.py) | | 62 | [Unique Paths](https://leetcode.com/problems/unique-paths/) | [solution.py](../problems/medium/unique_paths_62/solution.py) | [test_solution.py](../problems/medium/unique_paths_62/test_solution.py) | | 79 | [Word Search](https://leetcode.com/problems/word-search/) | [solution.py](../problems/medium/word_search_79/solution.py) | [test_solution.py](../problems/medium/word_search_79/test_solution.py) | | 102 | [Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/) | [solution.py](../problems/medium/binary_tree_level_order_traversal_102/solution.py) | [test_solution.py](../problems/medium/binary_tree_level_order_traversal_102/test_solution.py) | diff --git a/problems/medium/insert_interval_57/__init__.py b/problems/medium/insert_interval_57/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/problems/medium/insert_interval_57/solution.py b/problems/medium/insert_interval_57/solution.py new file mode 100644 index 0000000..ba2f3ff --- /dev/null +++ b/problems/medium/insert_interval_57/solution.py @@ -0,0 +1,39 @@ +# Tags: Array +from typing import List + + +class Solution: + def insert(self, intervals: List[List[int]], new_interval: List[int]) -> List[List[int]]: + """ + Insert new_interval into sorted intervals and merge overlapping intervals. + + Args: + intervals: List of non-overlapping intervals sorted by start time. + Each interval is [start, end] where start <= end. + new_interval: The interval to insert, formatted as [start, end]. + + Returns: + List of non-overlapping intervals sorted by start time after insertion + and merging all overlapping intervals. + + Time Complexity: O(n) + - Single pass through intervals list where n = len(intervals) + - Each iteration performs O(1) operations (comparisons, min/max) + - Worst case: process all intervals before finding insertion point + + Space Complexity: O(1) auxiliary space (excluding output) + - Only uses constant extra variables for iteration (i) + - Output list 'res' is not counted as auxiliary space + """ + res = [] + for i in range(len(intervals)): + if new_interval[1] < intervals[i][0]: + res.append(new_interval) + return res + intervals[i:] + elif new_interval[0] > intervals[i][1]: + res.append(intervals[i]) + else: + new_interval = [min(new_interval[0], intervals[i][0]), + max(new_interval[1], intervals[i][1])] + res.append(new_interval) + return res diff --git a/problems/medium/insert_interval_57/test_solution.py b/problems/medium/insert_interval_57/test_solution.py new file mode 100644 index 0000000..c0895ff --- /dev/null +++ b/problems/medium/insert_interval_57/test_solution.py @@ -0,0 +1,15 @@ +import pytest +from .solution import Solution +from tests.base_test import BaseTestSolution + + +class TestSolution(BaseTestSolution): + solution = Solution() + + @pytest.mark.parametrize("method_name, intervals, new_interval, expected, timeout", [ + ('insert', [[1, 3], [6, 9]], [2, 5], [[1, 5], [6, 9]], BaseTestSolution.DEFAULT_TIMEOUT), + ('insert', [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], [4, 8], [[1, 2], [3, 10], [12, 16]], + BaseTestSolution.DEFAULT_TIMEOUT), + ]) + def test_insert(self, method_name, intervals, new_interval, expected, timeout): + self._run_test(self.solution, method_name, (intervals, new_interval,), expected, timeout)