Skip to content

Commit b261b7c

Browse files
authored
Update rearranging_fruits.py
1 parent 7ba3fcf commit b261b7c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

greedy_methods/rearranging_fruits.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
from 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 fruits 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

5857
if __name__ == "__main__":
5958
import doctest
60-
6159
doctest.testmod()
6260
print("All doctests passed.")

0 commit comments

Comments
 (0)