Skip to content

Commit 14d22c0

Browse files
authored
Refactor docstring and type hints in merge_intervals
Updated type hints and docstring formatting in merge_intervals function.
1 parent 03c20b2 commit 14d22c0

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

data_structures/arrays/merge_intervals.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
2-
"""
2+
"""
33
Merge all overlapping intervals.
44
55
Each interval is represented as a list of two integers [start, end].
66
The function merges overlapping intervals and returns a list of
77
non-overlapping intervals sorted by start time.
88
99
Parameters:
10-
intervals (List[List[int]]): A list of intervals.
10+
intervals (list[list[int]]): A list of intervals.
1111
1212
Returns:
13-
List[List[int]]: A list of merged non-overlapping intervals.
13+
list[list[int]]: A list of merged non-overlapping intervals.
1414
15-
Edge Cases Handled:
15+
Edge Cases Handled:
1616
- Empty list: returns []
1717
- Single interval: returns the interval itself
1818
- Intervals already sorted or unsorted
1919
- Fully overlapping intervals
2020
- Invalid intervals (e.g., [[]] or intervals not having exactly 2 integers) raise ValueError
2121
22-
2322
Examples:
2423
>>> merge_intervals([[1, 3], [2, 6], [8, 10], [15, 18]])
2524
[[1, 6], [8, 10], [15, 18]]
@@ -33,19 +32,18 @@ def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
3332
[[1, 4]]
3433
3534
Time Complexity:
36-
O(n log n) sorting the intervals, where n is the number of intervals.
35+
O(n log n) - sorting the intervals, where n is the number of intervals.
3736
3837
Space Complexity:
39-
O(n) storing the merged intervals.
38+
O(n) - storing the merged intervals.
4039
"""
4140

4241
if not intervals:
4342
return []
4443
for interval in intervals:
44+
msg = f"Each interval must have exactly 2 integers, got {interval}"
4545
if len(interval) != 2:
46-
raise ValueError(
47-
f"Each interval must have exactly 2 integers, got {interval}"
48-
)
46+
raise ValueError(msg)
4947
# Sort intervals based on the start time
5048
intervals.sort(key=lambda interval: interval[0])
5149

0 commit comments

Comments
 (0)