@@ -0,0 +1,1681 @@
+package problems;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.List;%0A%0Apublic class MergeIntervals %7B%0A%0A    public class Interval %7B%0A        int start;%0A        int end;%0A%0A        Interval() %7B%0A            start = 0;%0A            end = 0;%0A        %7D%0A%0A        Interval(int s, int e) %7B%0A            start = s;%0A            end = e;%0A        %7D%0A    %7D%0A%0A    public List%3CInterval%3E merge(List%3CInterval%3E intervals) %7B%0A        Collections.sort(intervals, new IntervalStartComparator());%0A        for (int i = 1; i %3C intervals.size();) %7B%0A            if (intervals.get(i - 1).end %3E= intervals.get(i).start) %7B%0A                intervals.get(i - 1).end = Math.max(intervals.get(i).end, intervals.get(i - 1).end);%0A                intervals.remove(i);%0A            %7D else%0A                i++;%0A        %7D%0A        return intervals;%0A    %7D%0A%0A    class IntervalStartComparator implements Comparator%3CInterval%3E %7B%0A        @Override%0A        public int compare(Interval a, Interval b) %7B%0A            if (a.start %3C b.start)%0A                return -1;%0A            else if (a.start %3E b.start)%0A                return 1;%0A            else%0A                return 0;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        MergeIntervals mi = new MergeIntervals();%0A        List%3CInterval%3E intervals = new ArrayList%3CInterval%3E();%0A        intervals.add(mi.new Interval(1, 4));%0A        intervals.add(mi.new Interval(0, 2));%0A        intervals.add(mi.new Interval(3, 5));%0A%0A        List%3CInterval%3E out = mi.merge(intervals);%0A        for (int i = 0; i %3C intervals.size(); i++)%0A            System.out.print(%22 %5B%22 + out.get(i).start + %22, %22 + out.get(i).end + %22%5D%22);%0A    %7D%0A%7D%0A

Add Merger Intervals