-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution.cpp
48 lines (45 loc) · 1.08 KB
/
solution.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
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int solve(vector<int> &A, vector<int> &B, vector<int> &C);
};
int Solution::solve(vector<int> &A, vector<int> &B, vector<int> &C) {
int n1 = A.size();
int n2 = B.size();
int n3 = C.size();
int i = 0, j = 0, k = 0;
int res = INT_MAX;
while (i < n1 && j < n2 && k < n3) {
int mini = A[i], maxi = A[i];
if (B[j] < mini) {
mini = B[j];
}
if (B[j] > maxi) {
maxi = B[j];
}
if (C[k] < mini) {
mini = C[k];
}
if (C[k] > maxi) {
maxi = C[k];
}
res = min(res, maxi - mini);
if (A[i] == mini) {
++i;
} else if (B[j] == mini) {
++j;
} else {
++k;
}
}
return res;
}
int main() {
vector<int> A = {1, 2, 3, 4, 5};
vector<int> B = {6, 7, 8, 9, 10};
vector<int> C = {11, 12, 13, 14, 15};
Solution solver;
cout << solver.solve(A, B, C) << endl; // 6 because (5, 6, 11) is the triplet
}