-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathletcode-q11.java
More file actions
37 lines (25 loc) · 1.02 KB
/
letcode-q11.java
File metadata and controls
37 lines (25 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 {
public long minCost(final int[] basket1, final int[] basket2) {
final int n = basket1.length;
final Map<Integer, Integer> counts = new HashMap<>();
for(int i = 0; i < n; ++i)
counts.put(basket1[i], counts.getOrDefault(basket1[i], 0) + 1);
for(int i = 0; i < n; ++i)
counts.put(basket2[i], counts.getOrDefault(basket2[i], 0) - 1);
final List<Integer> swaps = new ArrayList<>();
int min = Integer.MAX_VALUE;
for(final Map.Entry<Integer, Integer> entry : counts.entrySet()) {
final int num = entry.getKey(), count = entry.getValue();
if(count % 2 > 0)
return -1;
min = Math.min(num, min);
for(int i = 0; i < Math.abs(count) / 2; ++i)
swaps.add(num);
}
Collections.sort(swaps);
long result = 0;
for(int i = 0; i < swaps.size() / 2; ++i)
result += Math.min(swaps.get(i), min * 2);
return result;
}
}