package com.fishercoder.solutions;

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

/**
 * 624. Maximum Distance in Arrays
 *
 * Given m arrays, and each array is sorted in ascending order.
 * Now you can pick up two integers from two different arrays (each array picks one)
 * and calculate the distance. We define the distance between two
 * integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.

 Example 1:
 Input:
 [[1,2,3],
 [4,5],
 [1,2,3]]

 Output: 4

 Explanation:

 One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.

 Note:
 Each given array will have at least 1 number. There will be at least two non-empty arrays.
 The total number of the integers in all the m arrays will be in the range of [2, 10000].
 The integers in the m arrays will be in the range of [-10000, 10000].
 */
public class _624 {

    public int maxDistance(int[][] arrays) {
        List<Integer> max = new ArrayList<>();
        for (int[] array : arrays) {
            max.add(array[array.length - 1]);
        }
        Collections.sort(max);
        int ans = Integer.MIN_VALUE;
        for (int[] array : arrays) {
            int big = array[array.length - 1] == max.get(max.size() - 1) ? max.get(max.size() - 2) : max.get(max.size() - 1);
            ans = Math.max(ans, big - array[0]);
        }
        return ans;
    }

}

@@ -948,16 +948,56 @@
 _624 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -1025,32 +1025,36 @@
 t%5B%5D%5B%5D arrays) %7B%0A
+    
         List%3CInt
@@ -1084,32 +1084,36 @@
 st%3C%3E();%0A        
+   
+ 
 for (int%5B%5D array
@@ -1117,32 +1117,36 @@
 ray : arrays) %7B%0A
+    
             max.
@@ -1183,26 +1183,33 @@
         
-%7D%0A
+    %7D%0A   
         
 Collecti
@@ -1200,16 +1200,17 @@
         
+ 
 Collecti
@@ -1224,16 +1224,20 @@
 t(max);%0A
+    
         
@@ -1265,32 +1265,36 @@
 _VALUE;%0A        
+ 
+   
 for (int%5B%5D array
@@ -1314,24 +1314,28 @@
             
+  
+  
 int big = ar
@@ -1448,16 +1448,20 @@
         
+    
 ans = Ma
@@ -1497,26 +1497,33 @@
         
-%7D%0A
+    %7D%0A   
         
 return a
@@ -1514,16 +1514,17 @@
         
+ 
 return a
@@ -1531,13 +1531,22 @@
 ns;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 624