11from collections import defaultdict
2- from typing import DefaultDict , List
32
43
5- def min_cost (basket1 : List [int ], basket2 : List [int ]) -> int :
4+ def min_cost (basket1 : list [int ], basket2 : list [int ]) -> int :
65 """
76 Compute the minimum cost to make two baskets identical by swapping fruits.
87
98 Each fruit is represented by an integer value. The goal is to make both baskets
109 have the same multiset of elements with the minimum swap cost. Each swap cost
11- is defined as the minimum of the swapped fruit’ s value and twice the smallest
10+ is defined as the minimum of the swapped fruit' s value and twice the smallest
1211 fruit value in both baskets.
1312
1413 If it's impossible to make the baskets identical, return -1.
1514
1615 Args:
17- basket1 (List [int]): The first basket of fruits.
18- basket2 (List [int]): The second basket of fruits.
16+ basket1 (list [int]): The first basket of fruits.
17+ basket2 (list [int]): The second basket of fruits.
1918
2019 Returns:
2120 int: The minimum total cost, or -1 if impossible.
@@ -33,15 +32,15 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int:
3332 -1
3433 """
3534 n = len (basket1 )
36- freq : DefaultDict [int , int ] = defaultdict (int )
35+ freq : defaultdict [int , int ] = defaultdict (int )
3736 mn : float = float ("inf" )
3837
3938 for i in range (n ):
4039 freq [basket1 [i ]] += 1
4140 freq [basket2 [i ]] -= 1
4241 mn = min (mn , basket1 [i ], basket2 [i ])
4342
44- to_swap : List [int ] = []
43+ to_swap : list [int ] = []
4544 for j , k in freq .items ():
4645 if k % 2 != 0 :
4746 return - 1
@@ -57,6 +56,5 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int:
5756
5857if __name__ == "__main__" :
5958 import doctest
60-
6159 doctest .testmod ()
6260 print ("All doctests passed." )
0 commit comments