Skip to content

322. Coin Change - #35

Open
akmhmgc wants to merge 1 commit into
mainfrom
322
Open

322. Coin Change#35
akmhmgc wants to merge 1 commit into
mainfrom
322

Conversation

@akmhmgc

@akmhmgc akmhmgc commented Sep 27, 2025

Copy link
Copy Markdown
Owner

解いた問題

322. Coin Change

使用言語

Ruby

次に解く問題

35. Search Insert Position

@akmhmgc akmhmgc added the ruby label Sep 27, 2025
@akmhmgc akmhmgc self-assigned this Sep 27, 2025
Comment thread 322/step3.md
fewest_num_coins[sub_amount] = [fewest_num_coins[sub_amount - coin] + 1, fewest_num_coins[sub_amount]].min
end
end
fewest_num_coins[amount] == Float::INFINITY ? not_existed : fewest_num_coins[amount]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

not_existedは変数に置かないでいいと思います。置くとしてもこの行の前でいいと思いました。

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.

コメントありがとうございます!
定数におきたいのですがRubyはメソッドの中で定数を定義できないので変数にしています。
(メソッドの外には定義できるが、スコープが広くなるのでやりたくない)

一方で、おっしゃる通り場所は直前で良いかもしれませんね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ruby っぽくないのかもしれませんが、

return -1 if fewest_num_coins[amount] == Float::INFINITY  # not existed
fewest_num_coins[amount]

くらいのほうが素直ではないでしょうか。

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/step3.md
fewest_num_coins[0] = 0
1.upto(amount).each do |sub_amount|
coins.each do |coin|
next if sub_amount - coin < 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.

どちらでもよいと思いますが、0から順にsub_amountを見ていってsub_amount + coin <= amountの場合にsub_amount + coinの場所を更新していく方法もあると思いました。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants