-
-
Notifications
You must be signed in to change notification settings - Fork 48.6k
rearranging fruit is an example of greedy algorithm #13291
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
Changes from 4 commits
816f99b
55f03bb
d62c263
73c8049
e316c4f
d6dcaf7
b5633f1
b69ef3c
63f5254
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import List | ||
Check failure on line 1 in dynamic_programming/coin_change_II.py
|
||
|
||
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 |
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
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the As there is no test file in this pull request nor any test function or class in the file |
||
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 |
There was a problem hiding this comment.
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
dynamic_programming/coin_change_II.py
, please provide doctest for the functionchange