[经典面试题][阿里]三元组最小距离

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## [经典面试题][阿里]三元组最小距离

sjf0115 2015-02-21 11:06:00 浏览1259

Distance = max(|a[ I ] – b[ j ]|, |a[ I ] – c[ k ]|, |b[ j ] – c[ k ]|)

``````    /*---------------------------------------------
*   日期：2015-02-21
*   作者：SJF0115
*   题目: 三元组最小距离
*   来源：阿里
*   博客：
-----------------------------------------------*/
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;

class Solution {
public:
int MinDistance(int a[],int l,int b[],int m,int c[],int n){
if(l <= 0 || n <= 0 || m <= 0){
return -1;
}//if
int min = INT_MAX;
int dis = 0,first = 0,second = 0,third = 0;
for(int i = 0,j = 0,k = 0;i < l && j < m && k < n;){
dis = max(max(abs(a[i]-b[j]),abs(a[i]-c[k])),abs(b[j]-c[k]));
if(dis < min){
min = dis;
first = i;
second = j;
third = k;
}//if
if(a[i] < b[j]){
if(a[i] < c[k]){
++i;
}//if
else{
++k;
}//else
}//if
else{
if(b[j] < c[k]){
++j;
}//if
else{
++k;
}//else
}//else
}//for
cout<<"First->"<<first<<" Second->"<<second<<" Third->"<<third<<endl;
return min;
}
};

int main() {
Solution solution;
int a[] = {5,16,20};
int b[] = {13,14,15,17,35};
int c[] = {19,22,24,29,32,42};
int l = 3,m = 5,n = 6;
int result = solution.MinDistance(a,l,b,m,c,n);
cout<<result<<endl;
}
``````

sjf0115
+ 关注