Skip to content

Commit 4b9a069

Browse files
gokul000410pre-commit-ci[bot]cclausspriya-sundaram-dev
authored
added merge interval problem on the array data structure (#14009)
* added merge interval problem on the array ds * removed typing which is deprecated * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor type hints in merge_intervals function Updated type hints from List to built-in list for compatibility. * Enhance merge_intervals function documentation Added edge cases handling and updated complexity notes. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor docstring and type hints in merge_intervals Updated type hints and docstring formatting in merge_intervals function. * Refactor merge_intervals function docstring Updated docstring formatting and improved error message clarity. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update data_structures/arrays/merge_intervals.py Co-authored-by: priya-sundaram-dev <oc-409d01@agentmail.to> * updating DIRECTORY.md --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: priya-sundaram-dev <oc-409d01@agentmail.to> Co-authored-by: cclauss <cclauss@users.noreply.github.com>
1 parent e49caf5 commit 4b9a069

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
* [Index 2D Array In 1D](data_structures/arrays/index_2d_array_in_1d.py)
270270
* [Kth Largest Element](data_structures/arrays/kth_largest_element.py)
271271
* [Median Two Array](data_structures/arrays/median_two_array.py)
272+
* [Merge Intervals](data_structures/arrays/merge_intervals.py)
272273
* [Merge Sorted](data_structures/arrays/merge_sorted.py)
273274
* [Monotonic Array](data_structures/arrays/monotonic_array.py)
274275
* [Pairs With Given Sum](data_structures/arrays/pairs_with_given_sum.py)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
def merge_intervals(intervals: list[list[int]]) -> list[list[int]]:
2+
"""
3+
Merge all overlapping intervals.
4+
5+
Each interval is represented as a list of two integers [start, end].
6+
The function merges overlapping intervals and returns a list of
7+
non-overlapping intervals sorted by start time.
8+
9+
Parameters:
10+
intervals (list[list[int]]): A list of intervals.
11+
12+
Returns:
13+
list[list[int]]: A list of merged non-overlapping intervals.
14+
15+
Edge Cases Handled:
16+
- Empty list: returns []
17+
- Single interval: returns the interval itself
18+
- Intervals already sorted or unsorted
19+
- Fully overlapping intervals
20+
- Invalid intervals (e.g., [[]] or intervals not having exactly
21+
2 integers) raise ValueError
22+
23+
Examples:
24+
>>> merge_intervals([[1, 3], [2, 6], [8, 10], [15, 18]])
25+
[[1, 6], [8, 10], [15, 18]]
26+
>>> merge_intervals([[1, 4], [4, 5]])
27+
[[1, 5]]
28+
>>> merge_intervals([[6, 8], [1, 3], [2, 4]])
29+
[[1, 4], [6, 8]]
30+
>>> merge_intervals([])
31+
[]
32+
>>> merge_intervals([[1, 4]])
33+
[[1, 4]]
34+
35+
Time Complexity:
36+
O(n log n) - sorting the intervals, where n is the number of intervals.
37+
38+
Space Complexity:
39+
O(n) - storing the merged intervals.
40+
"""
41+
42+
if not intervals:
43+
return []
44+
45+
for interval in intervals:
46+
msg = f"Each interval must have exactly 2 integers, got {interval}"
47+
if len(interval) != 2:
48+
raise ValueError(msg)
49+
50+
# Sort intervals based on the start time
51+
# Sort a copy so the caller's list (and its inner lists) are never mutated
52+
sorted_intervals = sorted(intervals, key=lambda interval: interval[0])
53+
54+
merged: list[list[int]] = [sorted_intervals[0][:]]
55+
56+
for current in sorted_intervals[1:]:
57+
last = merged[-1]
58+
59+
# If current interval overlaps with the last merged interval
60+
if current[0] <= last[1]:
61+
last[1] = max(last[1], current[1])
62+
else:
63+
merged.append(current[:])
64+
return merged

0 commit comments

Comments
 (0)