-
Notifications
You must be signed in to change notification settings - Fork 3
/
2561.cpp
57 lines (57 loc) · 1.59 KB
/
2561.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
49
50
51
52
53
54
55
56
57
class Solution {
public:
long long minCost(vector<int>& basket1, vector<int>& basket2) {
int minVal = INT_MAX;
long long res = 0;
map<int, int> mp;
map<int, int> mp1;
for (auto& num : basket1) {
mp[num]++;
mp1[num]++;
minVal = min(minVal, num);
}
for (auto& num : basket2) {
mp[num]++;
minVal = min(minVal, num);
}
for (auto& [num, c] : mp) {
if (c & 1) return -1;
}
vector<int> more;
vector<int> less;
for (auto& [num, c] : mp) {
int count = c / 2 - mp1[num];
if (count == 0) continue;
if (count > 0) {
for (int i = 0; i < abs(count); ++i) less.push_back(num);
}
else {
for (int i = 0; i < abs(count); ++i) more.push_back(num);
}
}
int n = less.size();
int lessIdx = 0;
int moreIdx = 0;
while (n--) {
if (more[moreIdx] < less[lessIdx]) {
if (more[moreIdx] == minVal) {
res += more[moreIdx];
}
else {
res += min(more[moreIdx], minVal * 2);
}
moreIdx++;
}
else {
if (less[lessIdx] == minVal) {
res += less[lessIdx];
}
else {
res += min(less[lessIdx], minVal * 2);
}
lessIdx++;
}
}
return res;
}
};