package com.fishercoder.solutions;

import com.fishercoder.common.classes.Interval;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 56. Merge Intervals
 *
 * Given a collection of intervals, merge all overlapping intervals.

 For example,
 Given [1,3],[2,6],[8,10],[15,18],
 return [1,6],[8,10],[15,18].
 */
public class _56 {

  public static class Solution1 {
    public List<Interval> merge(List<Interval> intervals) {
        if (intervals.size() <= 1) {
            return intervals;
        }

        Collections.sort(intervals, (o1, o2) -> 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;
    }
  }

}

@@ -77,16 +77,17 @@
 terval;%0A
+%0A
 import j
@@ -164,191 +164,8 @@
 t;%0A%0A
-/**%0A * 56. Merge Intervals%0A *%0A * 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 */%0A
 publ
@@ -180,16 +180,18 @@
  _56 %7B%0A%0A
+  
   public
@@ -216,24 +216,28 @@
 tion1 %7B%0A    
+  
+  
 public List%3C
@@ -288,16 +288,20 @@
         
+    
 if (inte
@@ -329,24 +329,28 @@
             
+  
+  
 return inter
@@ -363,16 +363,20 @@
         
+    
 %7D%0A%0A     
@@ -378,16 +378,20 @@
         
+  
+  
 Collecti
@@ -453,16 +453,20 @@
         
+    
 List%3CInt
@@ -506,16 +506,20 @@
         
+  
+  
 for (int
@@ -563,24 +563,28 @@
             
+    
 int start = 
@@ -615,24 +615,28 @@
             
+  
+  
 int end = in
@@ -655,16 +655,20 @@
 i).end;%0A
+    
         
@@ -751,16 +751,20 @@
         
+  
+  
 end = Ma
@@ -814,16 +814,20 @@
         
+    
 i++;%0A   
@@ -827,34 +827,42 @@
 ++;%0A            
+  
-%7D%0A
+  %7D%0A    
             resu
@@ -907,16 +907,20 @@
         
+  
+  
 i--;%0A   
@@ -924,26 +924,32 @@
         
-%7D%0A
+    %7D%0A  
         
 return r
@@ -936,24 +936,26 @@
 %7D%0A          
+  
 return resul
@@ -961,17 +961,23 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%0A%7D%0A

refactor 56