package com.fishercoder.solutions;

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

public class _56 {

    public static class Solution1 {
        /**
         * My completely original solution on 10/12/2021.
         */
        public int[][] merge(int[][] intervals) {
            List<int[]> list = new ArrayList<>();
            Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));//to avoid integer subtraction overflow
            for (int i = 0; i < intervals.length; i++) {
                int start = intervals[i][0];
                int end = intervals[i][1];
                while (i + 1 < intervals.length && intervals[i + 1][0] <= end) {
                    end = Math.max(intervals[i + 1][1], end);
                    i++;
                }
                list.add(new int[]{start, end});
            }
            int[][] ans = new int[list.size()][2];
            for (int i = 0; i < list.size(); i++) {
                ans[i] = list.get(i);
            }
            return ans;
        }
    }

}

@@ -388,16 +388,31 @@
 a, b) -%3E
+ a%5B0%5D != b%5B0%5D ?
  Integer
@@ -431,16 +431,46 @@
 %5D, b%5B0%5D)
+ : Integer.compare(b%5B1%5D, a%5B1%5D)
 );//to a
@@ -913,173 +913,52 @@
     
-int%5B%5D%5B%5D ans = new int%5Blist.size()%5D%5B2%5D;%0A            for (int i = 0; i %3C list.size(); i++) %7B%0A                ans%5Bi%5D = list.get(i);%0A            %7D%0A            return ans
+return list.toArray(new int%5Blist.size()%5D%5B2%5D)
 ;%0A  

refactor 56