Solving Leetcode Interviews in Seconds with AI: Rearranging Fruits
Introduction
In this blog post, we will explore how to solve the LeetCode problem "2561" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.
Problem Statement
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 and j, and swap the ith fruit of basket1 with the jth fruit of basket2. 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: Input: basket1 = [4,2,2,2], basket2 = [1,4,1,2] Output: 1 Explanation: 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. Example 2: Input: basket1 = [2,3,4,1], basket2 = [3,2,5,1] Output: -1 Explanation: It can be shown that it is impossible to make both the baskets equal. Constraints: basket1.length == basket2.length 1 <= basket1.length <= 105 1 <= basket1[i],basket2[i] <= 109
Explanation
Here's the breakdown of the solution and the Python code:
High-Level Approach:
- Count the occurrences of each fruit cost in both baskets. The difference in counts reveals which fruits need to be swapped.
- Identify the fruits that need to be moved from one basket to the other (excess fruits). It's impossible if any fruit has an odd total count.
- The optimal strategy is to swap each excess fruit with the smallest fruit cost available in either basket, or indirectly through the global minimum to minimise cost.
Complexity:
- Runtime Complexity: O(n log n), dominated by sorting the excess values.
- Storage Complexity: O(n), for storing counts and the excess values.
Code
from collections import Counter
def minCostToMakeBasketsEqual(basket1, basket2):
"""
Calculates the minimum cost to make two fruit baskets equal by swapping fruits.
Args:
basket1 (list): A list of integers representing the cost of fruits in basket1.
basket2 (list): A list of integers representing the cost of fruits in basket2.
Returns:
int: The minimum cost to make both baskets equal, or -1 if impossible.
"""
n = len(basket1)
count = Counter(basket1) + Counter(basket2)
diff = Counter()
for x in basket1:
diff[x] += 1
for x in basket2:
diff[x] -= 1
excess = []
total_swaps = 0
for fruit, c in diff.items():
if c % 2 != 0:
return -1 # Impossible to make equal
if c > 0:
for _ in range(c // 2):
excess.append(fruit)
min_cost = min(count.keys())
excess.sort()
cost = 0
for x in excess:
cost += min(x, 2 * min_cost)
return cost