2561. Rearranging Fruits
Description
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
andj
, and swap theith
fruit ofbasket1
with thejth
fruit ofbasket2
. - 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 -1
if impossible.
Example 1:
1 | Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2] |
Example 2:
1 | Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1] |
Constraints:
basket1.length == basket2.length
1 <= basket1.length <= 10^5
1 <= basket1[i],basket2[i]<= 10^9
Hints/Notes
- 2025/04/25 Q2
- greedy
- 0x3Fâs solution
Solution
Language: C++
1 | class Solution { |