624. Maximum Distance in Arrays
Given m arrays, and each array was sorted in ascending order. Now you can pick up to integers from, different arrays (each array picks one) and calculate the distance. We define the distance between the integers a and B to be their absolute difference |a-b|. Your task is to find the maximum distance.
Example 1:
Input:
[[[+] [[+],
[4,5], []
]
output:4
Explanation: One 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 would has at least 1 number. There'll is at least, non-empty arrays.
The total number of the integers in all the arrays is in the range of [2, 10000].
The integers in the M arrays is in the range of [-10000, 10000]. Solution
Each list in the list is ordered, so List.get (0) is the smallest, and List.get (List.size ()-1) is the largest.
Subtract min with the maximum value of the current array and subtract max with the minimum value. Updates the maximum and minimum values with the current list.
public class Solution {public
int maxdistance (list<list<integer>> arrays) {
if (arrays = null | | arr Ays.size () = = 0) {
return 0;
}
int result = Integer.min_value;
int max = arrays.get (0). Get (Arrays.get (0). Size ()-1);
int min = arrays.get (0). Get (0);
for (int i = 1; i < arrays.size (); i++) {
result = Math.max (result, Math.Abs (Arrays.get (i). Get (Arrays.get (i). Size () -1)-min);
result = Math.max (result, Math.Abs (Arrays.get (i). Get (0)-Max));
max = Math.max (max, Arrays.get (i). Get (Arrays.get (i). Size ()-1));
min = math.min (min, Arrays.get (i). Get (0));
}
return result;
}
}