2689. Rearranging Fruits
My accepted Python solution to LeetCode problem 2689, Rearranging Fruits, running in 117ms.
- Difficulty: Hard
- Python
- Runtime 117ms
- Memory 39.3MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 117ms, memory 39.3MB, accepted 2025-12-29.
class Solution:
def minCost(self, basket1: List[int], basket2: List[int]) -> int:
from collections import Counter
cnt = Counter()
for x in basket1:
cnt[x] += 1
for x in basket2:
cnt[x] -= 1
swaps = []
for fruit, diff in cnt.items():
if diff % 2 != 0:
return -1
for _ in range(abs(diff) // 2):
swaps.append(fruit)
swaps.sort()
min_fruit = min(min(basket1), min(basket2))
n = len(swaps)
cost = 0
for i in range(n // 2):
cost += min(swaps[i], 2 * min_fruit)
return cost