Skip to content

Commit af342fc

Browse files
committed
README + 56
1 parent 4b7057c commit af342fc

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

Array/56. Merge Intervals/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Time spent : 10 min (Self + see hint)
2+
3+
GIVEN: a collection of intervals
4+
5+
RETURNS: the merged array that merge all overlapping intervals
6+
7+
EXAMPLES:
8+
9+
Input:
10+
11+
```
12+
[1,3],[2,6],[8,10],[15,18]
13+
```
14+
15+
Output:[1,6],[8,10],[15,18]
16+
17+
18+
19+
Use sort to first sort all the intervals.
20+
21+
private class IntervalComparator implements Comparator<Interval> {
22+
@Override
23+
public int compare(Interval i1, Interval i2) {
24+
return i1.start < i2.start ? -1 : i1.start == i2.start ? 0 : 1;
25+
}
26+
}
27+
Go through the intervals. If last one's end is smaller than current interval's start, then add current interval.
28+
29+
Otherwise change last interval's end to the max of last interval's end or current interval's end.
30+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for an interval.
3+
* public class Interval {
4+
* int start;
5+
* int end;
6+
* Interval() { start = 0; end = 0; }
7+
* Interval(int s, int e) { start = s; end = e; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<Interval> merge(List<Interval> intervals) {
12+
Collections.sort(intervals, new IntervalComparator());
13+
LinkedList<Interval> merged = new LinkedList<>();
14+
15+
for (Interval interval : intervals) {
16+
if (merged.size() == 0 || merged.getLast().end < interval.start) {
17+
merged.add(interval);
18+
}
19+
else {
20+
merged.getLast().end = Math.max(merged.getLast().end, interval.end);
21+
}
22+
}
23+
return merged;
24+
}
25+
26+
private class IntervalComparator implements Comparator<Interval> {
27+
@Override
28+
public int compare(Interval i1, Interval i2) {
29+
return i1.start < i2.start ? -1 : i1.start == i2.start ? 0 : 1;
30+
}
31+
}
32+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
| 253 | 229 | [Majority Element II][229] | Java | Medium | 2018.1.6 | Array | |
258258
| 254 | 570 | [Managers with at Least 5 Direct Reports][570] | SQL | Medium | 2018.1.6 | DataBase | |
259259
| 255 | 596 | [Classes More Than 5 Students][596] | SQL | Easy | 2018.1.6 | DataBase | |
260+
| 256 | 56 | [Merge Intervals][56] | Java | Medium | 2018.1.6 | Array | |
260261

261262

262263
[1]:<https://leetcode.com/problems/two-sum/> "Two Sum"
@@ -289,6 +290,7 @@
289290
[51]:<https://leetcode.com/problems/n-queens/> "N-Queens"
290291
[52]:<https://leetcode.com/problems/n-queens-ii/> "N-Queens II"
291292
[53]:<https://leetcode.com/problems/maximum-subarray/> "Maximum Subarray"
293+
[56]:<https://leetcode.com/problems/merge-intervals/> "Merge Intervals"
292294
[58]:<https://leetcode.com/problems/length-of-last-word/> "Length of Last Word"
293295
[61]:<https://leetcode.com/problems/rotate-list/> "Rotate List"
294296
[62]:<https://leetcode.com/problems/unique-paths/> "Unique Paths"

0 commit comments

Comments
 (0)