Class Solution
java.lang.Object
g2501_2600.s2561_rearranging_fruits.Solution
2561 - Rearranging Fruits.<p>Hard</p>
<p>You have two fruit baskets containing <code>n</code> fruits each. You are given two <strong>0-indexed</strong> integer arrays <code>basket1</code> and <code>basket2</code> representing the cost of fruit in each basket. You want to make both baskets <strong>equal</strong>. To do so, you can use the following operation as many times as you want:</p>
<ul>
<li>Chose two indices <code>i</code> and <code>j</code>, and swap the <code>i<sup>th</sup></code> fruit of <code>basket1</code> with the <code>j<sup>th</sup></code> fruit of <code>basket2</code>.</li>
<li>The cost of the swap is <code>min(basket1[i],basket2[j])</code>.</li>
</ul>
<p>Two baskets are considered equal if sorting them according to the fruit cost makes them exactly the same baskets.</p>
<p>Return <em>the minimum cost to make both the baskets equal or</em> <code>-1</code> <em>if impossible.</em></p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> basket1 = [4,2,2,2], basket2 = [1,4,1,2]</p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong> 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.</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> basket1 = [2,3,4,1], basket2 = [3,2,5,1]</p>
<p><strong>Output:</strong> -1</p>
<p><strong>Explanation:</strong> It can be shown that it is impossible to make both the baskets equal.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>basket1.length == bakste2.length</code></li>
<li><code>1 <= basket1.length <= 10<sup>5</sup></code></li>
<li><code>1 <= basket1[i],basket2[i] <= 10<sup>9</sup></code></li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
minCost
public long minCost(int[] basket1, int[] basket2)
-