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
20 changes: 0 additions & 20 deletions Array/57. Insert Interval.py

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/MEDIUM.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Medium Problems

Total: 24 problems solved
Total: 25 problems solved

## Solutions

Expand All @@ -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) |
Expand Down
Empty file.
39 changes: 39 additions & 0 deletions problems/medium/insert_interval_57/solution.py
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions problems/medium/insert_interval_57/test_solution.py
Original file line number Diff line number Diff line change
@@ -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)
Loading