Skip to content
22 changes: 22 additions & 0 deletions greedy_methods/rearranging_fruits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution:
def minCost(self, basket1: List[int], basket2: List[int]) -> int:

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:2:52: F821 Undefined name `List`

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:2:32: F821 Undefined name `List`

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

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

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:2:52: F821 Undefined name `List`

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:2:32: F821 Undefined name `List`

Check failure on line 2 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

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

Choose a reason for hiding this comment

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

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

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

Choose a reason for hiding this comment

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

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

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

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)

Check failure on line 4 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:4:16: F821 Undefined name `defaultdict`

Check failure on line 4 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F821)

greedy_methods/rearranging_fruits.py:4:16: F821 Undefined name `defaultdict`
mn = float('inf')
for i in range(n):
freq[basket1[i]] += 1
freq[basket2[i]] -= 1
mn = min(mn, basket1[i], basket2[i])

Check failure on line 10 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:10:1: W293 Blank line contains whitespace

Check failure on line 10 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:10:1: W293 Blank line contains whitespace
to_swap = []
for j,k in freq.items():
if k % 2 != 0:
return -1
to_swap += [j] * (abs(k) // 2)

Check failure on line 16 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:16:1: W293 Blank line contains whitespace

Check failure on line 16 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:16:1: W293 Blank line contains whitespace
to_swap.sort()
res = 0
for i in range(len(to_swap) // 2):
res += min(to_swap[i], 2 * mn)

Check failure on line 21 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:21:1: W293 Blank line contains whitespace

Check failure on line 21 in greedy_methods/rearranging_fruits.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

greedy_methods/rearranging_fruits.py:21:1: W293 Blank line contains whitespace
return res
Loading