Skip to content
Closed
21 changes: 21 additions & 0 deletions dynamic_programming/coin_change_II.py
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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

dynamic_programming/coin_change_II.py:1:1: I001 Import block is un-sorted or un-formatted

Check failure on line 1 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

dynamic_programming/coin_change_II.py:1:1: UP035 `typing.List` is deprecated, use `list` instead

Check failure on line 1 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N999)

dynamic_programming/coin_change_II.py:1:1: N999 Invalid module name: 'coin_change_II'

class Solution:
def change(self, amount: int, coins: List[int]) -> int:

Check failure on line 4 in dynamic_programming/coin_change_II.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

dynamic_programming/coin_change_II.py:4:42: UP006 Use `list` instead of `List` for type annotation

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 function change

cache = {}
def dfs(i,a):

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 function dfs

Please provide return type hint for the function: dfs. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: i

Please provide type hint for the parameter: i

Please provide descriptive name for the parameter: a

Please provide type hint for the parameter: 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
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

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`
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
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
to_swap.sort()
res = 0
for i in range(len(to_swap) // 2):
res += min(to_swap[i], 2 * mn)

return res
Loading