-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathMinimize max(a-b,b-c,c-a).cpp
59 lines (39 loc) · 1.45 KB
/
Minimize max(a-b,b-c,c-a).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Given three arrays, select each number a, b, c from each array to minimize max(a-b,b-c,c-a)
*/
/*
solution: sort three arrays, from the beginning of each array, compare three elements, increase the array index of the smallest element.
O(n+m+l) time, O(1) space
*/
#include<iostream>
#include<cassert>
#include<algorithm>
using namespace std;
int GetMinDiff(int arr[], int arrlen, int barr[], int barrlen, int carr[], int carrlen) {
assert(arr && arrlen > 0 && barr && barrlen >0 && carr && carrlen > 0);
int arriter = 0;
int barriter = 0;
int carriter = 0;
int result = INT_MAX;
while (arriter < arrlen && barriter < barrlen && carriter < carrlen) {
int curdiff = max(max(abs(arr[arriter] - barr[barriter]), abs(arr[arriter] - carr[carriter])), abs(barr[barriter] - carr[carriter]));
result = min(curdiff, result);
if (res == 0) return 0;
if (a[arriter] <= b[barriter] && a[arriter] <= c[carriter]) {
arriter++;
} else if(b[barriter] <= a[arriter] && b[barriter] <= c[carriter]) {
barriter++;
} else {
carriter++;
}
}
return result;
}
int main() {
int a[] = {92, 13, 25, 26, 31, 31, 50, 52, 52, 58, 58, 69, 71, 87, 96};
int b[] = {6, 28, 28, 33, 38, 45, 46, 49, 67, 69, 71, 78, 84, 87, 98};
int c[] = {10, 18, 21, 34, 36, 46, 53, 54, 65, 72, 79, 80, 82, 85, 93};
int len = sizeof(a)/sizeof(a[0]);
cout<<GetMinDiff(a, len, b, len, c, len);
return 0;
}