package com.fishercoder.solutions;

import com.fishercoder.common.classes.Interval;

import java.util.ArrayList;
import java.util.List;

/**
 * 57. Insert Interval
 *
 * Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).

 You may assume that the intervals were initially sorted according to their start times.

 Example 1:
 Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

 Example 2:
 Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

 This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
 */
public class _57 {

  public static class Solution1 {
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
      List<Interval> result = new ArrayList<>();
      int i = 0;
      // add all the intervals ending before newInterval starts
      while (i < intervals.size() && intervals.get(i).end < newInterval.start) {
        result.add(intervals.get(i++));
      }
      // merge all overlapping intervals to one considering newInterval
      while (i < intervals.size() && intervals.get(i).start <= newInterval.end) {
        newInterval = new Interval( // we could mutate newInterval here also
            Math.min(newInterval.start, intervals.get(i).start),
            Math.max(newInterval.end, intervals.get(i).end));
        i++;
      }
      result.add(newInterval);
      // add all the rest
      while (i < intervals.size()) {
        result.add(intervals.get(i++));
      }
      return result;
    }
  }
}

@@ -134,505 +134,8 @@
 t;%0A%0A
-/**%0A * 57. Insert Interval%0A *%0A * Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).%0A%0A You may assume that the intervals were initially sorted according to their start times.%0A%0A Example 1:%0A Given intervals %5B1,3%5D,%5B6,9%5D, insert and merge %5B2,5%5D in as %5B1,5%5D,%5B6,9%5D.%0A%0A Example 2:%0A Given %5B1,2%5D,%5B3,5%5D,%5B6,7%5D,%5B8,10%5D,%5B12,16%5D, insert and merge %5B4,9%5D in as %5B1,2%5D,%5B3,10%5D,%5B12,16%5D.%0A%0A This is because the new interval %5B4,9%5D overlaps with %5B3,5%5D,%5B6,7%5D,%5B8,10%5D.%0A */%0A
 publ
@@ -150,16 +150,18 @@
  _57 %7B%0A%0A
+  
   public
@@ -186,16 +186,20 @@
 tion1 %7B%0A
+    
     publ
@@ -269,24 +269,30 @@
 Interval) %7B%0A
+      
       List%3CI
@@ -334,16 +334,22 @@
 ;%0A      
+    
+  
 int i = 
@@ -351,16 +351,22 @@
  i = 0;%0A
+      
       //
@@ -423,24 +423,30 @@
 tarts%0A      
+  
+    
 while (i %3C i
@@ -504,24 +504,32 @@
 al.start) %7B%0A
+        
         resu
@@ -562,24 +562,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 // merge
@@ -648,16 +648,22 @@
 nterval%0A
+      
       wh
@@ -732,24 +732,32 @@
 rval.end) %7B%0A
+        
         newI
@@ -829,24 +829,36 @@
             
+  
+          
 Math.min(new
@@ -906,24 +906,36 @@
             
+          
+  
 Math.max(new
@@ -968,24 +968,32 @@
 t(i).end));%0A
+        
         i++;
@@ -999,24 +999,36 @@
 ;%0A      
+ 
-%7D%0A
+     %7D%0A    
       
+  
 result.a
@@ -1044,16 +1044,22 @@
 erval);%0A
+      
       //
@@ -1078,24 +1078,30 @@
  rest%0A      
+  
+    
 while (i %3C i
@@ -1119,32 +1119,40 @@
 ze()) %7B%0A        
+      
+  
 result.add(inter
@@ -1173,24 +1173,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A  
       
+    
 return r
@@ -1212,16 +1212,22 @@
 lt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 56