-
-
Notifications
You must be signed in to change notification settings - Fork 50.7k
rearranging fruits #13319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
rearranging fruits #13319
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
816f99b
Create coin_change_II.py
Drag0nop 55f03bb
Merge pull request #1 from Drag0nop/Drag0nop-patch-1
Drag0nop d62c263
rearranging_fruits.py
Drag0nop 73c8049
Merge branch 'master' into Drag0nop-patch-2
Drag0nop e316c4f
Delete dynamic_programming/coin_change_II.py
Drag0nop d6dcaf7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b5633f1
Update rearranging_fruits.py
Drag0nop c308b24
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4849db3
Update rearranging_fruits.py
Drag0nop a8bfe51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c9da86c
Merge branch 'master' into Drag0nop-patch-2-1
Drag0nop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # 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 | ||
|
|
||
| def min_cost(basket1: List[int], basket2: List[int]) -> int: | ||
|
Check failure on line 13 in greedy_methods/rearranging_fruits.py
|
||
| 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)}") | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.