您的位置:首页 > 其它

Maximum Distance in Arrays

2017-06-20 00:00 288 查看
问题:

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].

解决:

① 链表所指向的二维数组中,每个数组最小的是第0个,最大的是第len-1个,并且同一个数组中的最大值和最小值不能同时出现。第一次想到的是直接遍历,超时。

public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int distance = 0;
for (int i = 0;i < arrays.size() ;i ++ ) {
min = arrays.get(i).get(0);
for (int j = 0;j < arrays.size() ;j ++ ) {
if (j == i) continue;
max = arrays.get(j).get(arrays.get(j).size() - 1);
distance = Integer.max(max - min,distance);
}
}
return distance;
}
}

②另一种想法是分别取出每组的最大值和最小值,使用两个map分别保存最大值和最小值,键---值,值---所在的数组下标,然后分别将最大值在和最小值数组排序,计算distance,只要保证它们的数组下标不同即可。若数组下标相同,则最大值或者最小值移动一个,就可以保证它们不属于同一数组。耗时29ms.

public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int min[] = new int[arrays.size()];
int max[] = new int[arrays.size()];
int distance = 0;
for (int i = 0;i < arrays.size() ;i ++ ) {
min[i] = arrays.get(i).get(0);
max[i] = arrays.get(i).get(arrays.get(i).size() - 1);
}
Map<Integer,Integer> minMap = new HashMap<>();
Map<Integer,Integer> maxMap = new HashMap<>();
for (int i = 0;i < arrays.size() ;i ++ ) {
minMap.put(min[i],i);
maxMap.put(max[i],i);
}
Arrays.sort(min);
Arrays.sort(max);
int p1 = 0;
int p2 = max.length - 1;
if (minMap.get(min[p1]) != maxMap.get(max[p2])) {//p1所指向的值的下标与p2所指向的值的下标不同
distance = Integer.max(distance,Math.abs(max[p2] - min[p1]));
}else{//若相同则最小值取下一个或最大值取下一个,注意,此时再取时一定不会相同了,所以不必再考虑之后
int d1 = 0;
int d2 = 0;
if(p1 + 1 < min.length){
d1 = Math.abs(max[p2] - min[p1 + 1]);
}
if(p2 - 1 >= 0){
d2 = Math.abs(max[p2 - 1] - min[p1]);
}
distance = Math.max(d1,d2);
}
return distance;
}
}

③ 先记录下第一个数组的第0个和第n-1个作为最大值和最小值,扫描剩下的数组,分别计算他们之间的差值即可,耗时23ms.

public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int distance = 0;
int min_val = arrays.get(0).get(0);
int max_val = arrays.get(0).get(arrays.get(0).size() - 1);
for(int i = 1;i < arrays.size();i ++){
distance = Math.max(distance,
Math.max(Math.abs(arrays.get(i).get(arrays.get(i).size() - 1) - min_val),
Math.abs(max_val - arrays.get(i).get(0))));
min_val = Math.min(min_val,arrays.get(i).get(0));
max_val = Math.max(max_val,arrays.get(i).get(arrays.get(i).size() - 1));
}
return distance;
}
}

④ 使用一个变量记录第二大的数和第二小的数,这样就不再需要专门去找一次了。15ms.

public class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
int secmin = Integer.MAX_VALUE;
int secmax = Integer.MIN_VALUE;
int minindex = -1;
int maxindex = -1;
for (int i = 0;i < arrays.size() ;i ++ ) {
int minnow = arrays.get(i).get(0);
int maxnow = arrays.get(i).get(arrays.get(i).size() - 1);
if (minnow <= min) {
secmin = min;
min = minnow;
minindex = i;
}else if (minnow < secmin) {
secmin = minnow;
}
if (maxnow >= max) {
secmax = max;
max = maxnow;
maxindex = i;
}else if (maxnow > secmax) {
secmax = maxnow;
}
}
return minindex == maxindex ? Math.max(secmax - min,max - secmin) : max - min;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: