Skip to content
37 changes: 37 additions & 0 deletions greedy_methods/rearranging_fruits.py
Original file line number Diff line number Diff line change
@@ -0,0 +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.

Check failure on line 1 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

greedy_methods/rearranging_fruits.py:1:205: W291 Trailing whitespace

Check failure on line 1 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

greedy_methods/rearranging_fruits.py:1:89: E501 Line too long (205 > 88)
# You want to make both baskets equal. To do so, you can use the following operation as many times as you want:

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

greedy_methods/rearranging_fruits.py:2:89: E501 Line too long (111 > 88)

# Chose two indices i and j, and swap the ith fruit of basket1 with the jth fruit of basket2.

Check failure on line 4 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

greedy_methods/rearranging_fruits.py:4:89: E501 Line too long (93 > 88)
# 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.

Check failure on line 6 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

greedy_methods/rearranging_fruits.py:6:89: E501 Line too long (115 > 88)

# Return the minimum cost to make both the baskets equal or -1 if impossible.

from typing import List

Check failure on line 10 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

greedy_methods/rearranging_fruits.py:10:1: UP035 `typing.List` is deprecated, use `list` instead
from collections import defaultdict

Check failure on line 11 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

greedy_methods/rearranging_fruits.py:10:1: I001 Import block is un-sorted or un-formatted

class Solution:
def minCost(self, basket1: List[int], basket2: List[int]) -> int:

Check failure on line 14 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

greedy_methods/rearranging_fruits.py:14:52: UP006 Use `list` instead of `List` for type annotation

Check failure on line 14 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

greedy_methods/rearranging_fruits.py:14:32: UP006 Use `list` instead of `List` for type annotation

Check failure on line 14 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

greedy_methods/rearranging_fruits.py:14:9: N802 Function name `minCost` should be lowercase

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: minCost

As there is no test file in this pull request nor any test function or class in the file greedy_methods/rearranging_fruits.py, please provide doctest for the function minCost

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