Skip to content
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
816f99b
Create coin_change_II.py
Drag0nop Oct 5, 2025
55f03bb
Merge pull request #1 from Drag0nop/Drag0nop-patch-1
Drag0nop Oct 6, 2025
d62c263
rearranging_fruits.py
Drag0nop Oct 6, 2025
73c8049
Merge branch 'master' into Drag0nop-patch-2
Drag0nop Oct 6, 2025
e316c4f
Delete dynamic_programming/coin_change_II.py
Drag0nop Oct 6, 2025
d6dcaf7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2025
b5633f1
Update rearranging_fruits.py
Drag0nop Oct 7, 2025
c308b24
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2025
4849db3
Update rearranging_fruits.py
Drag0nop Oct 7, 2025
a8bfe51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2025
c9da86c
Merge branch 'master' into Drag0nop-patch-2-1
Drag0nop Oct 7, 2025
0ad5696
Update rearranging_fruits.py
Drag0nop Oct 7, 2025
950a03f
Merge branch 'master' into Drag0nop-patch-2-1
Drag0nop Oct 8, 2025
1b78c6d
Update rearranging_fruits.py
Drag0nop Oct 8, 2025
19c0dfd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2025
0bbd49d
Update rearranging_fruits.py
Drag0nop Oct 8, 2025
faeb9ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2025
178c6da
Update rearranging_fruits.py
Drag0nop Oct 8, 2025
7ba3fcf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2025
b261b7c
Update rearranging_fruits.py
Drag0nop Oct 8, 2025
c79e215
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 8, 2025
e0bdd63
Merge branch 'master' into Drag0nop-patch-2-1
Drag0nop Oct 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions greedy_methods/rearranging_fruits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from collections import defaultdict


def min_cost(basket1: list[int], basket2: list[int]) -> int:
"""
Compute the minimum cost to make two baskets identical by swapping fruits.

Each fruit is represented by an integer value. The goal is to make both baskets
have the same multiset of elements with the minimum swap cost. Each swap cost
is defined as the minimum of the swapped fruit's value and twice the smallest
fruit value in both baskets.

If it's impossible to make the baskets identical, return -1.

Args:
basket1 (list[int]): The first basket of fruits.
basket2 (list[int]): The second basket of fruits.

Returns:
int: The minimum total cost, or -1 if impossible.

Examples:
>>> min_cost([4, 2, 2, 2], [1, 4, 1, 2])
1
>>> min_cost([1, 2, 3, 4], [2, 3, 4, 1])
0
>>> min_cost([1, 1, 1, 1], [1, 1, 1, 1])
0
>>> min_cost([1, 2, 2], [2, 1, 1])
-1
>>> min_cost([5, 3, 3, 2], [2, 5, 5, 3])
-1
"""
n = len(basket1)
freq: defaultdict[int, int] = defaultdict(int)
mn: float = float("inf")

for i in range(n):
freq[basket1[i]] += 1
freq[basket2[i]] -= 1
mn = min(mn, basket1[i], basket2[i])

to_swap: list[int] = []
for j, k in freq.items():
if k % 2 != 0:
return -1
to_swap += [j] * (abs(k) // 2)

to_swap.sort()
res: int = 0
for i in range(len(to_swap) // 2):
res += min(to_swap[i], 2 * int(mn))

return res


if __name__ == "__main__":
import doctest

doctest.testmod()
print("All doctests passed.")