From 816f99b53e5916df8557e73149cc7908209be8f1 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Sun, 5 Oct 2025 21:20:05 +0530 Subject: [PATCH 01/17] Create coin_change_II.py --- dynamic_programming/coin_change_II.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 dynamic_programming/coin_change_II.py diff --git a/dynamic_programming/coin_change_II.py b/dynamic_programming/coin_change_II.py new file mode 100644 index 000000000000..5581780b1139 --- /dev/null +++ b/dynamic_programming/coin_change_II.py @@ -0,0 +1,21 @@ +from typing import List + +class Solution: + def change(self, amount: int, coins: List[int]) -> int: + cache = {} + def dfs(i,a): + if a == amount: + return 1 + if a > amount: + return 0 + if i == len(coins): + return 0 + if (i,a) in cache: + return cache[(i,a)] + cache[(i,a)] = dfs(i,a + coins[i]) +dfs(i+1, a) + return cache[(i,a)] + return dfs(0,0) + +sol = Solution() +# for example +print(sol.change(5, [1,2,5])) # Output: 4 From d62c2636a21b1574fa32fdf1d0deec032fa28ee6 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Tue, 7 Oct 2025 00:25:37 +0530 Subject: [PATCH 02/17] rearranging_fruits.py example of a greedy algorithm --- greedy_methods/rearranging_fruits.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 greedy_methods/rearranging_fruits.py diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py new file mode 100644 index 000000000000..3d3e6db036c0 --- /dev/null +++ b/greedy_methods/rearranging_fruits.py @@ -0,0 +1,22 @@ +class Solution: + def minCost(self, basket1: List[int], basket2: List[int]) -> int: + n = len(basket1) + freq = defaultdict(int) + mn = float('inf') + for i in range(n): + freq[basket1[i]] += 1 + freq[basket2[i]] -= 1 + mn = min(mn, basket1[i], basket2[i]) + + to_swap = [] + for j,k in freq.items(): + if k % 2 != 0: + return -1 + to_swap += [j] * (abs(k) // 2) + + to_swap.sort() + res = 0 + for i in range(len(to_swap) // 2): + res += min(to_swap[i], 2 * mn) + + return res From e316c4f186a8b2c103febf113ca5fe57bc6a58a9 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Tue, 7 Oct 2025 00:47:40 +0530 Subject: [PATCH 03/17] Delete dynamic_programming/coin_change_II.py --- dynamic_programming/coin_change_II.py | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 dynamic_programming/coin_change_II.py diff --git a/dynamic_programming/coin_change_II.py b/dynamic_programming/coin_change_II.py deleted file mode 100644 index 5581780b1139..000000000000 --- a/dynamic_programming/coin_change_II.py +++ /dev/null @@ -1,21 +0,0 @@ -from typing import List - -class Solution: - def change(self, amount: int, coins: List[int]) -> int: - cache = {} - def dfs(i,a): - if a == amount: - return 1 - if a > amount: - return 0 - if i == len(coins): - return 0 - if (i,a) in cache: - return cache[(i,a)] - cache[(i,a)] = dfs(i,a + coins[i]) +dfs(i+1, a) - return cache[(i,a)] - return dfs(0,0) - -sol = Solution() -# for example -print(sol.change(5, [1,2,5])) # Output: 4 From d6dcaf7690193c2fb6f968cd7f35ad86de99c8af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 13:12:12 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 3d3e6db036c0..ec87d94c348f 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -2,21 +2,21 @@ class Solution: def minCost(self, basket1: List[int], basket2: List[int]) -> int: n = len(basket1) freq = defaultdict(int) - mn = float('inf') + mn = float("inf") for i in range(n): freq[basket1[i]] += 1 freq[basket2[i]] -= 1 mn = min(mn, basket1[i], basket2[i]) - + to_swap = [] - for j,k in freq.items(): + for j, k in freq.items(): if k % 2 != 0: return -1 to_swap += [j] * (abs(k) // 2) - + to_swap.sort() res = 0 for i in range(len(to_swap) // 2): res += min(to_swap[i], 2 * mn) - + return res From b5633f1dcf80b68f5c31de72ed205423764d9384 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Tue, 7 Oct 2025 18:53:07 +0530 Subject: [PATCH 05/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index ec87d94c348f..4bf6eba49561 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,22 +1,37 @@ +# it's a leetcode question no. 2561 where 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. + +from typing import List +from collections import defaultdict + class Solution: def minCost(self, basket1: List[int], basket2: List[int]) -> int: n = len(basket1) freq = defaultdict(int) - mn = float("inf") + mn = float('inf') for i in range(n): freq[basket1[i]] += 1 freq[basket2[i]] -= 1 mn = min(mn, basket1[i], basket2[i]) - + to_swap = [] - for j, k in freq.items(): + for j,k in freq.items(): if k % 2 != 0: return -1 to_swap += [j] * (abs(k) // 2) - + to_swap.sort() res = 0 for i in range(len(to_swap) // 2): res += min(to_swap[i], 2 * mn) - + return res + +s = Solution() +print(s.minCost([4, 2, 2, 2], [1, 4, 1, 2])) # Output: 1 From c308b24a44086526dc6c9a6e458443994bf7a3ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 13:30:10 +0000 Subject: [PATCH 06/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 4bf6eba49561..fb110388b6cc 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,4 +1,4 @@ -# it's a leetcode question no. 2561 where 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. +# it's a leetcode question no. 2561 where 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. @@ -10,28 +10,30 @@ from typing import List from collections import defaultdict + class Solution: def minCost(self, basket1: List[int], basket2: List[int]) -> int: n = len(basket1) freq = defaultdict(int) - mn = float('inf') + mn = float("inf") for i in range(n): freq[basket1[i]] += 1 freq[basket2[i]] -= 1 mn = min(mn, basket1[i], basket2[i]) - + to_swap = [] - for j,k in freq.items(): + for j, k in freq.items(): if k % 2 != 0: return -1 to_swap += [j] * (abs(k) // 2) - + to_swap.sort() res = 0 for i in range(len(to_swap) // 2): res += min(to_swap[i], 2 * mn) - + return res + s = Solution() print(s.minCost([4, 2, 2, 2], [1, 4, 1, 2])) # Output: 1 From 4849db300a45d001d4e4ea2f013d0ca3859e477e Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Tue, 7 Oct 2025 21:45:01 +0530 Subject: [PATCH 07/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 64 ++++++++++++++++------------ 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index fb110388b6cc..c4a98c90fd19 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -10,30 +10,40 @@ from typing import List from collections import defaultdict - -class Solution: - def minCost(self, basket1: List[int], basket2: List[int]) -> int: - n = len(basket1) - freq = defaultdict(int) - mn = float("inf") - for i in range(n): - freq[basket1[i]] += 1 - freq[basket2[i]] -= 1 - mn = min(mn, basket1[i], basket2[i]) - - to_swap = [] - for j, k in freq.items(): - if k % 2 != 0: - return -1 - to_swap += [j] * (abs(k) // 2) - - to_swap.sort() - res = 0 - for i in range(len(to_swap) // 2): - res += min(to_swap[i], 2 * mn) - - return res - - -s = Solution() -print(s.minCost([4, 2, 2, 2], [1, 4, 1, 2])) # Output: 1 +def min_cost(basket1: List[int], basket2: List[int]) -> int: + n = len(basket1) + freq = defaultdict(int) + mn = float("inf") + + for i in range(n): + freq[basket1[i]] += 1 + freq[basket2[i]] -= 1 + mn = min(mn, basket1[i], basket2[i]) + + to_swap = [] + for j, k in freq.items(): + if k % 2 != 0: + return -1 + to_swap += [j] * (abs(k) // 2) + + to_swap.sort() + res = 0 + for i in range(len(to_swap) // 2): + res += min(to_swap[i], 2 * mn) + + return res + + +if __name__ == "__main__": + # ---- Test Cases ---- + test_cases = [ + ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 + ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 + ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 + ([1, 2, 2], [2, 1, 1]), # Expected: -1 + ([5, 3, 3, 2], [2, 5, 5, 3]) # Expected: -1 + ] + + print("Running test cases...\n") + for b1, b2 in test_cases: + print(f"basket1 = {b1}, basket2 = {b2} → minCost = {min_cost(b1, b2)}") From a8bfe51d0a1fdffa2a6c426c8b51acfb5fee990f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 16:15:21 +0000 Subject: [PATCH 08/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index c4a98c90fd19..b8caaa31fcf1 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -10,6 +10,7 @@ from typing import List from collections import defaultdict + def min_cost(basket1: List[int], basket2: List[int]) -> int: n = len(basket1) freq = defaultdict(int) @@ -37,11 +38,11 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: if __name__ == "__main__": # ---- Test Cases ---- test_cases = [ - ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 - ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 - ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 - ([1, 2, 2], [2, 1, 1]), # Expected: -1 - ([5, 3, 3, 2], [2, 5, 5, 3]) # Expected: -1 + ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 + ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 + ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 + ([1, 2, 2], [2, 1, 1]), # Expected: -1 + ([5, 3, 3, 2], [2, 5, 5, 3]), # Expected: -1 ] print("Running test cases...\n") From 0ad56962e529e3d7c5bbfc589efec9e919ff4079 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Tue, 7 Oct 2025 23:28:34 +0530 Subject: [PATCH 09/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index b8caaa31fcf1..59bf33fd70e0 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,12 +1,3 @@ -# it's a leetcode question no. 2561 where 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. - from typing import List from collections import defaultdict From 1b78c6d7b5e655127123a490b6b251bf186a5966 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Wed, 8 Oct 2025 17:14:33 +0530 Subject: [PATCH 10/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 59bf33fd70e0..d6ce5c2e1cb3 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,8 +1,6 @@ -from typing import List from collections import defaultdict - -def min_cost(basket1: List[int], basket2: List[int]) -> int: +def min_cost(basket1: list[int], basket2: list[int]) -> int: n = len(basket1) freq = defaultdict(int) mn = float("inf") From 19c0dfd7e311f51ec0cf60027af79d508de047b3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 11:44:52 +0000 Subject: [PATCH 11/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index d6ce5c2e1cb3..88947dd2d022 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,5 +1,6 @@ from collections import defaultdict + def min_cost(basket1: list[int], basket2: list[int]) -> int: n = len(basket1) freq = defaultdict(int) From 0bbd49d93b0dbe1a35c74e45862775c1ab54012f Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Wed, 8 Oct 2025 17:29:26 +0530 Subject: [PATCH 12/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 88947dd2d022..1a64154c78f5 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,26 +1,28 @@ from collections import defaultdict +from typing import DefaultDict, List -def min_cost(basket1: list[int], basket2: list[int]) -> int: +def min_cost(basket1: List[int], basket2: List[int]) -> int: n = len(basket1) - freq = defaultdict(int) - mn = float("inf") + 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 = [] + 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 = 0 + res: int = 0 for i in range(len(to_swap) // 2): - res += min(to_swap[i], 2 * mn) + # Ensure mn is treated as int during arithmetic + res += min(to_swap[i], 2 * int(mn)) return res @@ -31,7 +33,7 @@ def min_cost(basket1: list[int], basket2: list[int]) -> int: ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 - ([1, 2, 2], [2, 1, 1]), # Expected: -1 + ([1, 2, 2], [2, 1, 1]), # Expected: -1 ([5, 3, 3, 2], [2, 5, 5, 3]), # Expected: -1 ] From faeb9cad5708bd64451267b2f83e3b8940668fbe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 11:59:47 +0000 Subject: [PATCH 13/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 1a64154c78f5..27b5b55cae5f 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -33,7 +33,7 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 - ([1, 2, 2], [2, 1, 1]), # Expected: -1 + ([1, 2, 2], [2, 1, 1]), # Expected: -1 ([5, 3, 3, 2], [2, 5, 5, 3]), # Expected: -1 ] From 178c6dae854cd8db2ce5859a734096ac934d3642 Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Wed, 8 Oct 2025 17:30:45 +0530 Subject: [PATCH 14/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 45 ++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 27b5b55cae5f..3da62505963a 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -3,6 +3,35 @@ 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") @@ -21,22 +50,12 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: to_swap.sort() res: int = 0 for i in range(len(to_swap) // 2): - # Ensure mn is treated as int during arithmetic res += min(to_swap[i], 2 * int(mn)) return res if __name__ == "__main__": - # ---- Test Cases ---- - test_cases = [ - ([4, 2, 2, 2], [1, 4, 1, 2]), # Expected: 1 - ([1, 2, 3, 4], [2, 3, 4, 1]), # Expected: 0 - ([1, 1, 1, 1], [1, 1, 1, 1]), # Expected: 0 - ([1, 2, 2], [2, 1, 1]), # Expected: -1 - ([5, 3, 3, 2], [2, 5, 5, 3]), # Expected: -1 - ] - - print("Running test cases...\n") - for b1, b2 in test_cases: - print(f"basket1 = {b1}, basket2 = {b2} → minCost = {min_cost(b1, b2)}") + import doctest + doctest.testmod() + print("All doctests passed.") From 7ba3fcf1f3fdc50aa10eb8a32550093d3f36ed8a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 12:01:54 +0000 Subject: [PATCH 15/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 3da62505963a..6449e58db985 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -57,5 +57,6 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod() print("All doctests passed.") From b261b7cf4d73ab3b255f04f0a7aa81729951473d Mon Sep 17 00:00:00 2001 From: Krishna Singh Date: Wed, 8 Oct 2025 17:34:16 +0530 Subject: [PATCH 16/17] Update rearranging_fruits.py --- greedy_methods/rearranging_fruits.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 6449e58db985..0ba48070028f 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -1,21 +1,20 @@ from collections import defaultdict -from typing import DefaultDict, List -def min_cost(basket1: List[int], basket2: List[int]) -> int: +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 + 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. + 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. @@ -33,7 +32,7 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: -1 """ n = len(basket1) - freq: DefaultDict[int, int] = defaultdict(int) + freq: defaultdict[int, int] = defaultdict(int) mn: float = float("inf") for i in range(n): @@ -41,7 +40,7 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: freq[basket2[i]] -= 1 mn = min(mn, basket1[i], basket2[i]) - to_swap: List[int] = [] + to_swap: list[int] = [] for j, k in freq.items(): if k % 2 != 0: return -1 @@ -57,6 +56,5 @@ def min_cost(basket1: List[int], basket2: List[int]) -> int: if __name__ == "__main__": import doctest - doctest.testmod() print("All doctests passed.") From c79e215b2acea823b2637f2f4a90549f5a9e0b56 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 8 Oct 2025 12:04:39 +0000 Subject: [PATCH 17/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- greedy_methods/rearranging_fruits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/greedy_methods/rearranging_fruits.py b/greedy_methods/rearranging_fruits.py index 0ba48070028f..35257add1d30 100644 --- a/greedy_methods/rearranging_fruits.py +++ b/greedy_methods/rearranging_fruits.py @@ -56,5 +56,6 @@ def min_cost(basket1: list[int], basket2: list[int]) -> int: if __name__ == "__main__": import doctest + doctest.testmod() print("All doctests passed.")