Conversation
| 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 |
There was a problem hiding this comment.
math.isinf() も選択肢としてありそうですね。infinityは普通の値と扱いが異なるので、個人的には値をそのまま比べるよりも math.isinf() を使いたい気持ちがあります、が好みの問題だと思います。
There was a problem hiding this comment.
専用の関数がある場合には使うべきですね。math.isinf()を以降覚えておこうと思います。
| @@ -0,0 +1,13 @@ | |||
| class Solution: | |||
| def coinChange(self, coins: List[int], amount: int) -> int: | |||
There was a problem hiding this comment.
https://docs.python.org/3/library/typing.html#typing.List に
Deprecated alias to list.
とあるように、古いバージョンを扱う場合以外は list をそのまま型ヒントとして使用したい気持ちがあります。
There was a problem hiding this comment.
調べてみるとtypes.GenericAliasというものみたいですね。listに引数を与えてlist[int]とするとtypes.GenericAliasになるようです。
https://docs.python.org/3/library/stdtypes.html#types-genericalias
| num_changes = [float("inf")] * (amount + 1) | ||
| num_changes[0] = 0 |
There was a problem hiding this comment.
2行目で何となく想像は付くのですが、添字の説明がコメントであると親切に思いました。
There was a problem hiding this comment.
the minimum number of coins required to make up amount i
としました。確かに初見ではわかりにくいかもしれないです。
| coins_sorted = sorted(coins) | ||
|
|
||
| frontier = deque([0]) | ||
| visited = {0} |
There was a problem hiding this comment.
visit済みの何なのか分かりにくいので、命名で補足すると良さそうです。visited_sum ですかね。
https://leetcode.com/problems/coin-change/