-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
37 lines (30 loc) · 1.02 KB
/
Copy pathsolution.java
File metadata and controls
37 lines (30 loc) · 1.02 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
class Solution {
int[] smallestDiff(int a[], int b[], int c[]) {
Arrays.sort(a);
Arrays.sort(b);
Arrays.sort(c);
int i = 0, j = 0, k = 0;
long bestDiff = Long.MAX_VALUE;
long bestSum = Long.MAX_VALUE;
int[] ans = new int[3];
while (i < a.length && j < b.length && k < c.length) {
int x = a[i], y = b[j], z = c[k];
int mx = Math.max(x, Math.max(y, z));
int mn = Math.min(x, Math.min(y, z));
long diff = mx - mn;
long sum = (long)x + y + z;
if (diff < bestDiff || (diff == bestDiff && sum < bestSum)) {
bestDiff = diff;
bestSum = sum;
ans[0] = x;
ans[1] = y;
ans[2] = z;
}
if (mn == x) i++;
else if (mn == y) j++;
else k++;
}
Arrays.sort(ans);
return new int[]{ans[2], ans[1], ans[0]}; // decreasing order
}
}