You have two fruit baskets containing n fruits each. You are given two
0-indexed integer arrays basket1 and basket2 representing the cost of fruit in each basket. You want to make both baskets equal. To do so, you can use the following operation as many times as you want:
Chose two indices i and j, and swap the ith fruit of basket1 with the jth fruit of basket2.
The cost of the swap is min(basket1[i],basket2[j]).
Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.
Return the minimum cost to make both the baskets equal or-1if impossible.
Input: basket1 =[4,2,2,2], basket2 =[1,4,1,2]Output: 1Explanation: Swap index 1 of basket1 with index 0 of basket2, which has cost 1. Now basket1 =[4,1,2,2] and basket2 =[2,4,1,2]. Rearranging both the arrays makes them equal.
If the total count of each fruit is not even, it’s impossible. Otherwise, we can pair up the excess fruits in each basket and swap them, always using the cheapest possible swap (either direct or via the global minimum fruit).
#include<unordered_map>classSolution {
public:longlong minCost(vector<int>& b1, vector<int>& b2) {
unordered_map<int, int> cnt;
for (int x : b1) cnt[x]++;
for (int x : b2) cnt[x]--;
vector<int> pos, neg;
int mn = INT_MAX;
for (auto& [k, v] : cnt) {
mn = min(mn, k);
if (v %2!=0) return-1;
if (v >0) for (int i =0; i < v/2; ++i) pos.push_back(k);
if (v <0) for (int i =0; i <-v/2; ++i) neg.push_back(k);
}
sort(pos.begin(), pos.end());
sort(neg.rbegin(), neg.rend());
longlong ans =0;
for (int i =0; i < pos.size(); ++i) {
ans += min({pos[i], neg[i], 2*mn});
}
return ans;
}
};
import java.util.*;
classSolution {
publiclongminCost(int[] b1, int[] b2) {
Map<Integer, Integer> cnt =new HashMap<>();
int mn = Integer.MAX_VALUE;
for (int x : b1) { cnt.put(x, cnt.getOrDefault(x, 0) + 1); mn = Math.min(mn, x); }
for (int x : b2) { cnt.put(x, cnt.getOrDefault(x, 0) - 1); mn = Math.min(mn, x); }
List<Integer> pos =new ArrayList<>(), neg =new ArrayList<>();
for (var e : cnt.entrySet()) {
int k = e.getKey(), v = e.getValue();
if (v % 2 != 0) return-1;
for (int i = 0; i < Math.abs(v)/2; ++i) {
if (v > 0) pos.add(k);
if (v < 0) neg.add(k);
}
}
Collections.sort(pos);
neg.sort(Collections.reverseOrder());
long ans = 0;
for (int i = 0; i < pos.size(); ++i) {
ans += Math.min(Math.min(pos.get(i), neg.get(i)), 2*mn);
}
return ans;
}
}
classSolution {
funminCost(b1: IntArray, b2: IntArray): Long {
val cnt = mutableMapOf<Int, Int>()
var mn = Int.MAX_VALUE
for (x in b1) { cnt[x] = cnt.getOrDefault(x, 0) + 1; mn = minOf(mn, x) }
for (x in b2) { cnt[x] = cnt.getOrDefault(x, 0) - 1; mn = minOf(mn, x) }
val pos = mutableListOf<Int>()
val neg = mutableListOf<Int>()
for ((k, v) in cnt) {
if (v % 2!=0) return -1 repeat(kotlin.math.abs(v)/2) {
if (v > 0) pos.add(k)
if (v < 0) neg.add(k)
}
}
pos.sort()
neg.sortDescending()
var ans = 0Lfor (i in pos.indices) {
ans += minOf(pos[i], neg[i], 2*mn)
}
return ans
}
}
classSolution:
defminCost(self, basket1: list[int], basket2: list[int]) -> int:
from collections import Counter
cnt = Counter()
mn = float('inf')
for x in basket1:
cnt[x] +=1 mn = min(mn, x)
for x in basket2:
cnt[x] -=1 mn = min(mn, x)
pos, neg = [], []
for k, v in cnt.items():
if v %2!=0:
return-1if v >0:
pos += [k] * (v //2)
if v <0:
neg += [k] * (-v //2)
pos.sort()
neg.sort(reverse=True)
ans =0for x, y in zip(pos, neg):
ans += min(x, y, 2*mn)
return ans