-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3pointers.cpp
More file actions
41 lines (41 loc) · 1.26 KB
/
3pointers.cpp
File metadata and controls
41 lines (41 loc) · 1.26 KB
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
#include<math.h>
#include<vector>
#include<climits>
int fun(int num1, int num2, int num3){
int diff1 = abs(num1-num2);
int diff2 = abs(num2-num3);
int diff3 = abs(num2-num2);
int returnValue = std::max(diff1, std::max(diff2, diff3));
return returnValue;
}
int minimize(const std::vector<int> &A, const std::vector<int> &B, const std::vector<int> &C){
int AIndex = 0;
int returnValue = INT_MAX;
while(AIndex<A.size()){
int AVal = A[AIndex], BIndex = 0,CIndex = 0;
while(BIndex<B.size() && CIndex<C.size()){
int BVal = B[BIndex],CVal = C[CIndex];
returnValue = std::min(returnValue,fun(AVal,BVal,CVal));
if(BVal<CVal){
++BIndex;
}
else{
++CIndex;
}
}
while(BIndex<B.size()){
//C has reached its limits
int CVal = C[C.size()-1];
returnValue = std::min(returnValue,fun(AVal,B[BIndex],CVal));
++BIndex;
}
while(CIndex<C.size()){
//B has reached its limits
int BVal = B[B.size()-1];
returnValue = std::min(returnValue,fun(AVal,BVal,C[CIndex]));
++CIndex;
}
++AIndex;
}
return returnValue;
}