Skip to content

Conversation

Satorien
Copy link
Owner

@Satorien Satorien commented Aug 9, 2025

if i + coin > amount:
continue
if min_coins_to_amount[i + coin] == -1 or \
min_coins_to_amount[i + coin] > num_coins + 1:
Copy link

Choose a reason for hiding this comment

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

この条件分岐の方法は思いつきませんでした。minなどの比較をしなくてよくなるのでシンプルになっていいですね!

Copy link

Choose a reason for hiding this comment

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

min_coins_to_amount の値を大きな数字で初期化すれば、 if 文が不要になると思います。

INFINITY = 100000
min_coins_to_amount = [0] + [INFINITY] * amount
for i, num_coins in enumerate(min_coins_to_amount):
    if num_coins == -1:
        continue
    for coin in coins:
        if i + coin > amount:
            continue
        min_coins_to_amount[i + coin] = min(min_coins_to_amount[i + coin], num_coins + 1)
if min_coins_to_amount[-1] == INFINITY:
    return -1
return min_coins_to_amount[-1]

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かに最後に一回分岐作るだけの方が良さそうですね

if amount < 0:
continue
if amount == 0:
return 0
Copy link

Choose a reason for hiding this comment

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

amountのチェックはfor分岐の前に持ってきたほうが自然でしょうか。

```python
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
remaining_to_min_coin = [None] * amount + [0]
Copy link

Choose a reason for hiding this comment

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

同一の list の中に、 None と 0 という異なる型の値が含まれている点に違和感を感じました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

そうなんですね、同じ型で比較できる形の方が自然なんですね

Copy link

Choose a reason for hiding this comment

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

remaining_to_min_coin = [None] * (amount + 1)
remaining_to_min_coin[amount] = 0

の方が
一気に違う型を入れて初期化するよりは違和感は少ないかもしれません

min_coins = \
min(min_coins, numCoinsToAmount(amount - coin) + 1)
return min_coins
num_coins = numCoinsToAmount(amount)
Copy link

Choose a reason for hiding this comment

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

関数の切れ目が認識しづらいため、個人的には inner function のあとに空行を入れたいです。

if i + coin > amount:
continue
if min_coins_to_amount[i + coin] == -1 or \
min_coins_to_amount[i + coin] > num_coins + 1:
Copy link

Choose a reason for hiding this comment

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

min_coins_to_amount の値を大きな数字で初期化すれば、 if 文が不要になると思います。

INFINITY = 100000
min_coins_to_amount = [0] + [INFINITY] * amount
for i, num_coins in enumerate(min_coins_to_amount):
    if num_coins == -1:
        continue
    for coin in coins:
        if i + coin > amount:
            continue
        min_coins_to_amount[i + coin] = min(min_coins_to_amount[i + coin], num_coins + 1)
if min_coins_to_amount[-1] == INFINITY:
    return -1
return min_coins_to_amount[-1]

if remaining_to_min_coin[remaining] is None: continue
for coin in coins:
if coin > remaining: continue
for i in range(remaining // coin, 0, -1):
Copy link

Choose a reason for hiding this comment

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

indexでないものにiを使うのはやや違和感があるかもしれません

class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
@cache
def numCoinsToAmount(amount: int) -> int:
Copy link

Choose a reason for hiding this comment

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

https://google.github.io/styleguide/pyguide.html#316-naming
LeetCodeは何故か従ってないのですが、Pythonの関数名はnum_coins_to_amountのような表記が一般的そうです。
チームのルールによって異なるとは思います。

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.

4 participants