You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,20 +19,20 @@ Explanation: It can be shown that it is impossible to make both baskets equal.
19
19
```
20
20
21
21
## Approach
22
-
**Key Insight**: To make both baskets equal, the total sum of both baskets must be equal. If they're not equal, it's impossible. If they are equal, we need to find the minimum cost to rearrange fruits.
22
+
**Key Insight**: To make both baskets equal, we need to balance the fruits between them. The key optimization is using the minimum value in both baskets to potentially reduce swap costs.
23
23
24
24
**Algorithm**:
25
-
1. Check if the sum of both baskets is equal. If not, return -1.
26
-
2. Count the frequency of each fruit in both baskets.
27
-
3. For each fruit type, if the total count is odd, return -1 (impossible to split equally).
28
-
4. Calculate the excess/deficit for each fruit type.
29
-
5. Sort the excess and deficit arrays.
30
-
6. Use two pointers to match the smallest excess with the smallest deficit, minimizing the cost.
25
+
1. Count the frequency of each fruit in both baskets and find the minimum value.
26
+
2. For each fruit type, check if the total count is odd (impossible to split equally).
27
+
3. Calculate excess fruits for each basket (fruits that need to be moved out).
28
+
4. Sort one list in ascending order and the other in descending order for optimal matching.
29
+
5. Calculate the minimum cost considering both direct swaps and using the minimum value as an intermediary.
31
30
32
31
**Why this works**:
33
32
- We need to balance the fruits between baskets
34
-
- The minimum cost is achieved by matching the smallest excess with the smallest deficit
35
-
- Each swap operation costs the minimum of the two fruits being swapped
33
+
- The minimum cost is achieved by matching optimally
34
+
- Using the minimum value as an intermediary can sometimes reduce costs (2 * minVal vs direct swap)
35
+
- Sorting in opposite directions ensures optimal pairing
36
36
37
37
## Complexity Analysis
38
38
-**Time Complexity**: O(n log n) - Due to sorting the excess and deficit arrays
@@ -58,60 +58,56 @@ import java.util.*;
58
58
59
59
classSolution {
60
60
publiclongminCost(int[] basket1, int[] basket2) {
61
-
// Check if sums are equal
62
-
long sum1 =0, sum2 =0;
63
-
for (int fruit : basket1) sum1 += fruit;
64
-
for (int fruit : basket2) sum2 += fruit;
65
-
66
-
if (sum1 != sum2) return-1;
67
-
68
-
// Count frequencies
69
-
Map<Integer, Integer> freq1 =newHashMap<>();
70
-
Map<Integer, Integer> freq2 =newHashMap<>();
71
-
72
-
for (int fruit : basket1) freq1.put(fruit, freq1.getOrDefault(fruit, 0) +1);
73
-
for (int fruit : basket2) freq2.put(fruit, freq2.getOrDefault(fruit, 0) +1);
74
-
75
-
// Check if each fruit type has even total count
76
-
Set<Integer> allFruits =newHashSet<>();
77
-
allFruits.addAll(freq1.keySet());
78
-
allFruits.addAll(freq2.keySet());
79
-
80
-
for (int fruit : allFruits) {
81
-
int total = freq1.getOrDefault(fruit, 0) + freq2.getOrDefault(fruit, 0);
0 commit comments