Maximum Distance in Arrays
Givenm
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 integersa
andb
to be their absolute difference|a-b|
. Your task is to find the maximum distance.
Example 1:
注意:两个值不能从同一个array里取出,所以先比较结果,后更新start和wend,否则可能出现start和end在同一个array取出的情况
用两个变量start和end分别表示当前遍历过的数组中最小的首元素,和最大的尾元素,那么每当我们遍历到一个新的数组时,只需计算新数组尾元素和start绝对差,跟end和新数组首元素的绝对差,取二者之间的较大值来更新结果res即可,参见代码如下:
Last updated
Was this helpful?