Skip to content

Commit cba57ce

Browse files
merge intervals
1 parent 9a7bb96 commit cba57ce

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

HARD/src/hard/MergeIntervals.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package hard;
2+
import java.util.*;
3+
import classes.Interval;
4+
import utils.CommonUtils;
5+
6+
/**
7+
* Created by fishercoder1534 on 10/3/16.
8+
*/
9+
public class MergeIntervals {
10+
11+
/**Inspired by this post: https://discuss.leetcode.com/topic/4319/a-simple-java-solution
12+
* 1. Sort the intervals first, based on their starting point
13+
* 2. then compare the end point with next interval's start point, if they overlap, then update the end point to the longest one,
14+
* if they don't overlap, we add it into the result and continue the iteration.*/
15+
public static List<Interval> merge(List<Interval> intervals) {
16+
if(intervals.size() <= 1) return intervals;
17+
18+
Collections.sort(intervals, new Comparator<Interval>() {
19+
@Override
20+
public int compare(Interval o1, Interval o2) {
21+
return o1.start - o2.start;
22+
}
23+
});
24+
25+
List<Interval> result = new ArrayList();
26+
for(int i = 0; i < intervals.size(); i++){
27+
int start = intervals.get(i).start;
28+
int end = intervals.get(i).end;
29+
while(i < intervals.size() && end >= intervals.get(i).start){
30+
end = Math.max(end, intervals.get(i).end);
31+
i++;
32+
}
33+
result.add(new Interval(start, end));
34+
i--;
35+
}
36+
return result;
37+
}
38+
39+
public static void main(String[] args){
40+
List<Interval> list = new ArrayList<Interval>();
41+
// //test case 1:
42+
// list.add(new Interval(2,3));
43+
// list.add(new Interval(5,5));
44+
// list.add(new Interval(2,2));
45+
// list.add(new Interval(3,4));
46+
// list.add(new Interval(3,4));
47+
48+
//test case 2:
49+
list.add(new Interval(1,3));
50+
list.add(new Interval(2,6));
51+
list.add(new Interval(8,10));
52+
list.add(new Interval(15,18));
53+
CommonUtils.printList(merge(list));
54+
}
55+
56+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[Solution](../../blob/master/MEDIUM/src/medium/CloneGraph.java)| O(n)|O(n) | Medium| HashMap, BFS
77
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[Solution](../../blob/master/MEDIUM/src/medium/DecodeWays.java)| O(n)|O(n) | Medium| DP
88
|76|[Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/)|[Solution](../../blob/master/HARD/src/hard/MinimumWindowSubstring.java)|O(n)|O(k)|Hard|Two Pointers
9+
|56|[Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[Solution](../../blob/master/HARD/src/hard/MergeIntervals.java)|O(n*logn)|O(1)|Hard|
910
|23|[Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/)|[Solution](../../blob/master/HARD/src/hard/MergeKSortedList.java)|O(n*logk)|O(logk)|Hard|Heap
1011
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)|[Solution](../../blob/master/MEDIUM/src/medium/LetterCombinationsofaPhoneNumber.java)|O(n*4^n)|O(n)|Medium|Backtracking
1112
|10|[Regular Expression Matching](https://leetcode.com/problems/regular-expression-matching/)|[Solution](../../blob/master/HARD/src/hard/RegularExpressionMatching.java)|O(m*n)|O(m*n)|Hard|DP

0 commit comments

Comments
 (0)