-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path888.cpp
More file actions
22 lines (21 loc) · 690 Bytes
/
888.cpp
File metadata and controls
22 lines (21 loc) · 690 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
vector<int> fairCandySwap(vector<int>& aliceSizes, vector<int>& bobSizes) {
unordered_map<int, int> mp;
int sumAlice = accumulate(aliceSizes.begin(), aliceSizes.end(), 0);
int sumBob = accumulate(bobSizes.begin(), bobSizes.end(), 0);
for (auto& num : aliceSizes) {
mp[sumAlice - num * 2] = num;
}
for (auto& num : bobSizes) {
if (mp.find(sumBob - 2 * num) != mp.end()) {
return {mp[sumBob - 2 * num], num};
}
}
return {};
}
};
// N + a = M + b; exchange a, b
// N + b = sumN || M + a = sumM
// N - b = M - a
// sumN - 2 * b == sumM - 2 * a