Skip to content

Create 322. Coin Change.md#40

Open
fuga-98 wants to merge 1 commit into
mainfrom
322.-Coin-Change
Open

Create 322. Coin Change.md#40
fuga-98 wants to merge 1 commit into
mainfrom
322.-Coin-Change

Conversation

@fuga-98
Copy link
Copy Markdown
Owner

@fuga-98 fuga-98 commented May 5, 2025

Comment thread 322. Coin Change.md
if amount < 0:
return -1

def possible_coin():
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この形は内包表記とせいうち演算子でも書けそうですね。

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.

レビューありがとうございます。

                    if (num_coins := coin_change_helper(amount - coin)) == -1:
                        continue
                    yield num_coins + 1

セイウチだとこういう感じですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

想定はこんな感じです。
(num_coins + 1 for coin in coins if (num_coins := coin_change_helper(coin)) != -1)

Comment thread 322. Coin Change.md
if amount == 0:
return 0
if amount < 0:
return -1
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの範囲だと思いますが、amountの大きさの順番で処理がなされる方が自然な気がします。

if amount < 0:
~~~~
if amount == 0:
~~~~
# amount > 0の時の処理

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.

レビューありがとうございます!
その視点はありませんでした!

Comment thread 322. Coin Change.md
return 0
possible_amounts = [0]
num_coin = 0
checked = set()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

処理には影響しませんが、checked.add()のタイミング的に、ここでchecked = set([0])としたくなります。

Comment thread 322. Coin Change.md
continue
yield num_coins + 1

return min(possible_coin(), default=-1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

possible_coin関数は内部でcoin_change_helper関数を呼び、coin_change_helper関数は内部でpossible_coin関数を呼ぶ、という依存関係は認知負荷が高いように感じました。

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.

レビューありがとうございます。
確かに私も最初は読みにくいと感じました。
二つの関数で再帰していくという感じですね。

Comment thread 322. Coin Change.md

def possible_coin():
for coin in coins:
num_coins = coin_change_helper(amount - coin)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

好みの問題かと思いますが、私はnum_coins = coin_change_helper(amount - coin) + 1としたくなります。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

で、yield num_coinsです。

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.

考えましたが、-1のケースに足すのに抵抗があったのでこの書き方にしました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

あ、そういうことですね。失礼しました。

Comment thread 322. Coin Change.md

総当たり

総当たりは最大100^12くらい行きそうなので無理そう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

これはどのように推定したのでしょうか?

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.

ざっくり最も時間がかかりそうなケースで、
amount が10^4程度で、coinの枚数が12枚、コインの値が1~12の時は、10^4 / 12 = 100で、それが12個あるから12を累乗しました。
今考えると、もっと計算量がかかりそうだと思いました。

Constraints:
1 <= coins.length <= 12
1 <= coins[i] <= 2^31 - 1
0 <= amount <= 10^4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

10^4 / 12 = 100

833 くらいではないでしょうか。

自分なら
amount = 10^4
coins = [1, 2, ..., 12]
として、ある金額から 12 通りの分岐があるため、 10000^12 または数桁小さいと見積もると思います。

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.

ありがとうございます。
無理だろうなと思って結論ありきで計算していたので間違いに気づけませんでした。

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.

5 participants