Skip to content

Commit f6749e7

Browse files
Create 0056-merge-intervals.md
Added Lc Sol for 56
1 parent c0a15cb commit f6749e7

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
id: merge-intervals
3+
title: Merge Intervals(LeetCode)
4+
sidebar_label: 0056-Merge Intervals
5+
tags:
6+
- Array
7+
- Sorting
8+
description: Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
9+
---
10+
11+
## Problem Statement
12+
13+
Given an array of `intervals` where `intervals[i] = [starti, endi]`, merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
19+
```plaintext
20+
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
21+
Output: [[1,6],[8,10],[15,18]]
22+
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
23+
```
24+
25+
**Example 2:**
26+
27+
```plaintext
28+
Input: intervals = [[1,4],[4,5]]
29+
Output: [[1,5]]
30+
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
31+
```
32+
33+
### Constraints
34+
35+
- `1 <= intervals.length <= 104`
36+
- `intervals[i].length == 2`
37+
- `0 <= starti <= endi <= 104`
38+
39+
## Solution
40+
41+
### Approach
42+
43+
#### Algorithm
44+
45+
1. Edge Case Handling:
46+
* If the input list of intervals is empty or contains only one interval, return the input as there is nothing to merge.
47+
2. Sorting:
48+
* Sort the intervals based on their starting points. This makes it easier to merge overlapping intervals as they will be adjacent.
49+
3. Merging Intervals:
50+
* Initialize a new interval with the first interval and start iterating through the sorted intervals.
51+
* For each interval, check if it overlaps with the current new interval (i.e., the start of the current interval is less than or equal to the end of the new interval).
52+
* If they overlap, update the end of the new interval to be the maximum of the current interval's end and the new interval's end.
53+
* If they don't overlap, add the current new interval to the result list and start a new interval with the current interval.
54+
4. Final Step:
55+
* After the loop, add the last interval to the result list as it won't be added inside the loop.
56+
57+
#### Implementation
58+
59+
Efficient In-Place Solution with Arrays in Java
60+
61+
```Java
62+
import java.util.ArrayList;
63+
import java.util.Arrays;
64+
import java.util.List;
65+
66+
class Solution {
67+
public int[][] merge(int[][] intervals) {
68+
if (intervals.length <= 1)
69+
return intervals;
70+
71+
// Sort by ascending starting point
72+
Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
73+
74+
List<int[]> result = new ArrayList<>();
75+
int[] newInterval = intervals[0];
76+
result.add(newInterval);
77+
78+
for (int[] interval : intervals) {
79+
if (interval[0] <= newInterval[1]) { // Overlapping intervals, move the end if needed
80+
newInterval[1] = Math.max(newInterval[1], interval[1]);
81+
} else { // Disjoint intervals, add the new interval to the list
82+
newInterval = interval;
83+
result.add(newInterval);
84+
}
85+
}
86+
87+
return result.toArray(new int[result.size()][]);
88+
}
89+
}
90+
```
91+
92+
Previous Version Using Lists
93+
94+
```Java
95+
import java.util.Collections;
96+
import java.util.LinkedList;
97+
import java.util.List;
98+
99+
public class Interval {
100+
int start;
101+
int end;
102+
103+
Interval() { start = 0; end = 0; }
104+
Interval(int s, int e) { start = s; end = e; }
105+
}
106+
107+
public class Solution {
108+
public List<Interval> merge(List<Interval> intervals) {
109+
if (intervals.size() <= 1)
110+
return intervals;
111+
112+
// Sort by ascending starting point using an anonymous Comparator
113+
Collections.sort(intervals, (i1, i2) -> Integer.compare(i1.start, i2.start));
114+
115+
List<Interval> result = new LinkedList<>();
116+
int start = intervals.get(0).start;
117+
int end = intervals.get(0).end;
118+
119+
for (Interval interval : intervals) {
120+
if (interval.start <= end) // Overlapping intervals, move the end if needed
121+
end = Math.max(end, interval.end);
122+
else { // Disjoint intervals, add the previous one and reset bounds
123+
result.add(new Interval(start, end));
124+
start = interval.start;
125+
end = interval.end;
126+
}
127+
}
128+
129+
// Add the last interval
130+
result.add(new Interval(start, end));
131+
return result;
132+
}
133+
}
134+
```
135+
136+
### Complexity Analysis
137+
138+
- **Time complexity**: $O(NlogN)$
139+
- **Space complexity**: $O(N)$
140+
141+
### Conclusion
142+
143+
The presented solution efficiently merges overlapping intervals using sorting and iteration. The overall time complexity is O(n log n) due to the sorting step, and the space complexity is optimized by reusing the input array for storing the result. This method ensures a fast and memory-efficient approach to solving the interval merging problem.

0 commit comments

Comments
 (0)