Question
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.
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].
Explanation
这道题我是暴力解吧,先找到每个array里面的max和min,然后再得到相减最大的值。 在Discuss里面看到很棒的解法,我忽略了是sorted这个条件,在后面附上Discuss里面的解法,就是遍历的时候就同时计算了,非常巧妙。
Code
More Clever Solution: https://discuss.leetcode.com/topic/92859/java-solution-min-and-max
Stupid Solution: