package stevesun.algorithms;

import stevesun.common.classes.Interval;
import stevesun.common.utils.CommonUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by fishercoder1534 on 10/3/16.
 */
public class MergeIntervals {

    /**Inspired by this post: https://discuss.leetcode.com/topic/4319/a-simple-java-solution
     * 1. Sort the intervals first, based on their starting point
     * 2. then compare the end point with next interval's start point, if they overlap, then update the end point to the longest one,
     * if they don't overlap, we add it into the result and continue the iteration.*/
    public static List<Interval> merge(List<Interval> intervals) {
        if(intervals.size() <= 1) return intervals;

        Collections.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                return o1.start - o2.start;
            }
        });

        List<Interval> result = new ArrayList();
        for(int i = 0; i < intervals.size(); i++){
            int start = intervals.get(i).start;
            int end = intervals.get(i).end;
            while(i < intervals.size() && end >= intervals.get(i).start){
                end = Math.max(end, intervals.get(i).end);
                i++;
            }
            result.add(new Interval(start, end));
            i--;
        }
        return result;
    }

    public static void main(String[] args){
        List<Interval> list = new ArrayList<Interval>();
//        //test case 1:
//    	list.add(new Interval(2,3));
//    	list.add(new Interval(5,5));
//    	list.add(new Interval(2,2));
//    	list.add(new Interval(3,4));
//    	list.add(new Interval(3,4));

        //test case 2:
        list.add(new Interval(1,3));
        list.add(new Interval(2,6));
        list.add(new Interval(8,10));
        list.add(new Interval(15,18));
        CommonUtils.printList(merge(list));
    }

}

@@ -229,460 +229,188 @@
 %0A * 
-Created by fishercoder1534 on 10/3/16.%0A */%0Apublic class MergeIntervals %7B%0A%0A    /**Inspired by this post: https://discuss.leetcode.com/topic/4319/a-simple-java-solution%0A     * 1. Sort the intervals first, based on their starting point%0A     * 2. then compare the end point with next interval's start point, if they overlap, then update the end point to the longest one,%0A     * if they don't overlap, we add it into the result and continue the iteration.*/
+Given a collection of intervals, merge all overlapping intervals.%0A%0A For example,%0A Given %5B1,3%5D,%5B2,6%5D,%5B8,10%5D,%5B15,18%5D,%0A return %5B1,6%5D,%5B8,10%5D,%5B15,18%5D.%0A */%0Apublic class MergeIntervals %7B%0A
 %0A   

edit Merge Intervals