Friday, November 23, 2018
888. Fair Candy Swap
59.55 %
class Solution {
public int[] fairCandySwap(int[] A, int[] B) {
if (A == null || B == null) {
return new int[0];
}
Set<Integer> set = new HashSet<>();
int sumA = 0;
int sumB = 0;
for (int a : A) {
sumA += a;
set.add(a);
}
for (int b : B) {
sumB += b;
}
int diff = (sumA - sumB) / 2;
for (int b : B) {
if (set.contains(b + diff)) {
return new int[]{b + diff, b};
}
}
return new int[0];
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment