Skip to content

322. Coin Change#38

Open
tom4649 wants to merge 2 commits intomainfrom
322.Coin-Change
Open

322. Coin Change#38
tom4649 wants to merge 2 commits intomainfrom
322.Coin-Change

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Mar 29, 2026

return min([minimum_coins(target_amount - coin) + 1 for coin in coins])

num_changes = minimum_coins(amount)
return -1 if num_changes == float("inf") else num_changes
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

math.isinf() も選択肢としてありそうですね。infinityは普通の値と扱いが異なるので、個人的には値をそのまま比べるよりも math.isinf() を使いたい気持ちがあります、が好みの問題だと思います。

Copy link
Copy Markdown
Owner Author

@tom4649 tom4649 Mar 31, 2026

Choose a reason for hiding this comment

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

専用の関数がある場合には使うべきですね。math.isinf()を以降覚えておこうと思います。

@@ -0,0 +1,13 @@
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/typing.html#typing.List

Deprecated alias to list.

とあるように、古いバージョンを扱う場合以外は list をそのまま型ヒントとして使用したい気持ちがあります。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

調べてみるとtypes.GenericAliasというものみたいですね。listに引数を与えてlist[int]とするとtypes.GenericAliasになるようです。
https://docs.python.org/3/library/stdtypes.html#types-genericalias

Comment on lines +3 to +4
num_changes = [float("inf")] * (amount + 1)
num_changes[0] = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2行目で何となく想像は付くのですが、添字の説明がコメントであると親切に思いました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

the minimum number of coins required to make up amount i

としました。確かに初見ではわかりにくいかもしれないです。

coins_sorted = sorted(coins)

frontier = deque([0])
visited = {0}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

visit済みの何なのか分かりにくいので、命名で補足すると良さそうです。visited_sum ですかね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants