title : Merge Interval
Difficulty : Medium
topic content :
Given a collection of intervals, merge all overlapping intervals.
translation :
Given a set of intervals, merge all overlapping intervals.
Example 1:
Input: [[1,3],[2,6],[8,10],[15,18]]output: [[1,6],[8,10],[15,18]]
Example 2:
Input: [[1,4],[4,5]]output: [[1,5]]
my train of thought : Because may be disorderly order, the bad one is compared, so need to sort
1. Sort the elements in the list (using comparators), then compare them from the back to the next;
2. The current end and the next start to see if a merge is required
3, if need to merge then the current end is equal to its own and the next end of the larger, and then the next remove () out
My Code :
1 classSolution {2 PublicList<interval> Merge (list<interval>intervals) {3Collections.sort (intervals,Newmycomparator ());4 5 for(inti = 0; I < Intervals.size ()-1; i++) {6 if(Intervals.get (i). End >= Intervals.get (i+1). Start) {7Intervals.get (i). End = Intervals.get (i). End > Intervals.get (i+1). End? Intervals.get (i). End:intervals.get (i+1). End;8Intervals.remove (i+1);9i--;Ten } One } A returnintervals; - } - } the - classMycomparatorImplementsComparator<interval> { - - Public intCompare (Interval one, Interval) { + returnOne.start-Two.start; - } + A}
my complexity : O (NLOGN) is mostly sort of there with Nlogn left only n
problems in the programming process :
1, forget the comparator how to achieve :
Method One: implement the class in the list comparable<e> interface (the subject of E is interval), and override the CompareTo method (an incoming object) Compare your own value with the value of the incoming object.
"Comparison rules: they are larger than the incoming object returns zero, equals equal to 0, less than 0, so generally return directly to themselves-the incoming object" as follows:
Public int compareTo (Interval o) { return start- o.start; }
However, the interval of the subject is to write dead can not be modified in the editing, but do not worry, there is another way:
Method Two: write a class to implement the comparator<e> interface , and override the compare method (two incoming objects)
"Comparison rule: similar to the above generally directly return the previous object-the following object" is as follows:
Public int Compare (Interval one, Interval) { return one.start- two.start; }
Extension: If you want to compare two attributes, such as first compare names, then compare age ———— code as follows
Public int Compare (person one, person one) { int// Compare First string if If the name is the same, continue to compare the age of return one.age- two.age; Else // first compare the name, the name is not the same, then return the comparison results return i; }
using this method requires passing in the sort () method as the second parameter (so you can generally use the anonymous class directly in the Sort method) as follows:
New Comparator<interval>() { @Override publicint Compare (Interval Interval1, Interval interval2) { return interval1.start- interval2.start; } });
2, found that need to merge, is not directly after the end to take the end, because the end of the latter is also likely to be smaller than the current, such as "1,6", "3,5", so need to determine the size of the end;
3, because the order is backward loop, the list calls the Remove method after the next one, I will point to the latter to continue the loop, but at this point should still point to themselves, continue to determine whether the next need to merge, so I--.
Extension: What if it is looped back and forth? Need i + +?
At this point, the subscript i-1 is deleted, then I--, I move to subscript i-1, and the subscript i-1 is the subscript i before remove so no longer need to do the operation.
For example: Subscript 0,1,2 at this time I equals 2, the 1remove (at this time subscript 2 automatically becomes 1), and then I--, I still point to the original 2 position.
Therefore, when calling the list's Remove method in a loop, be sure to consider the subscript change, and adjust the value of the pointer i .
Answer code :
1 classSolution {2 Private classIntervalcomparatorImplementsComparator<interval> {3 @Override4 Public intCompare (Interval A, Interval b) {5 returnA.start < B.start? -1:a.start = = B.start? 0:1;6 }7 }8 9 PublicList<interval> Merge (list<interval>intervals) {TenCollections.sort (intervals,Newintervalcomparator ()); One ALinkedlist<interval> merged =NewLinkedlist<interval>(); - for(Interval interval:intervals) { - if(Merged.isempty () | | merged.getlast (). End <Interval.start) { the Merged.add (interval); - } - Else { -Merged.getlast (). End =Math.max (Merged.getlast (). end, interval.end); + } - } + A returnmerged; at } -}
The answer : And my ideas are the same, first sort and then from the beginning to determine whether to merge,
However, he used the queue and increased the complexity of the space, but this avoids the extra time complexity that the Remove method brings, both of which are beneficial.
Leetcode [56] (Java): Merge intervals