package com.fishercoder.solutions;

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

public class _56 {

    public static class Solution1 {
        public int[][] merge(int[][] intervals) {
            if (intervals.length <= 1) {
                return intervals;
            }

            Arrays.sort(intervals, (o1, o2) -> o1[0] - o2[0]);

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

}

@@ -174,139 +174,181 @@
     
-public int%5B%5D%5B%5D merge(int%5B%5D%5B%5D intervals) %7B%0A            if (intervals.length %3C= 1) %7B%0A                return intervals;%0A            %7D%0A
+/**%0A         * My completely original solution on 10/12/2021.%0A         */%0A        public int%5B%5D%5B%5D merge(int%5B%5D%5B%5D intervals) %7B%0A            List%3Cint%5B%5D%3E list = new ArrayList%3C%3E();
 %0A   
@@ -384,83 +384,57 @@
 s, (
-o1, o2
+a, b
 ) -%3E 
-o1
+a
 %5B0%5D 
-- o2%5B0%5D);%0A%0A            List%3Cint%5B%5D%3E result = new ArrayList(
+!= b%5B0%5D ? a%5B0%5D - b%5B0%5D : a%5B1%5D - b%5B1%5D
 );%0A 
@@ -602,16 +602,20 @@
 hile (i 
++ 1 
 %3C interv
@@ -631,15 +631,8 @@
 h &&
- end %3E=
  int
@@ -639,20 +639,31 @@
 ervals%5Bi
-%5D%5B0%5D
+ + 1%5D%5B0%5D %3C= end
 ) %7B%0A    
@@ -697,13 +697,8 @@
 max(
-end, 
 inte
@@ -704,20 +704,29 @@
 ervals%5Bi
-%5D%5B1%5D
+ + 1%5D%5B1%5D, end
 );%0A     
@@ -779,21 +779,19 @@
         
-resul
+lis
 t.add(ne
@@ -816,29 +816,8 @@
 %7D);%0A
-                i--;%0A
     
@@ -846,27 +846,19 @@
 int%5B%5D%5B%5D 
-finalResult
+ans
  = new i
@@ -860,21 +860,19 @@
 new int%5B
-resul
+lis
 t.size()
@@ -909,21 +909,19 @@
  0; i %3C 
-resul
+lis
 t.size()
@@ -949,95 +949,28 @@
     
-finalResult%5Bi%5D%5B0%5D = result.get(i)%5B0%5D;%0A                finalResult%5Bi%5D%5B1%5D = resul
+ans%5Bi%5D = lis
 t.get(i)
 %5B1%5D;
@@ -965,19 +965,16 @@
 t.get(i)
-%5B1%5D
 ;%0A      
@@ -1004,19 +1004,11 @@
 urn 
-finalResult
+ans
 ;%0A  

refactor 56