Skip to content

Practice Rearranging Fruits

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2561
LeetCode Problem

Rearranging Fruits

Problem Statement

basket1 and basket2 list equal numbers of fruit costs. A move swaps one fruit from each basket at a cost equal to the smaller of their current values. Return the minimum total cost to make the two baskets have identical value multisets, or -1 if impossible.

Source: Rearranging Fruits on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
basket1 = [4,2,2,2], basket2 = [1,4,1,2]
Output
1

# Example 2

Input
basket1 = [2,3,4,1], basket2 = [3,2,5,1]
Output
-1

Constraints

basket1.length == basket2.length
1 <= basket1.length <= 10^5
1 <= basket1[i], basket2[i] <= 10^9

Related LeetCode Problems

Practice more problems similar to Rearranging Fruits.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.