Conversation
potrue
reviewed
Sep 29, 2025
| 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] |
There was a problem hiding this comment.
not_existedは変数に置かないでいいと思います。置くとしてもこの行の前でいいと思いました。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます!
定数におきたいのですがRubyはメソッドの中で定数を定義できないので変数にしています。
(メソッドの外には定義できるが、スコープが広くなるのでやりたくない)
一方で、おっしゃる通り場所は直前で良いかもしれませんね。
There was a problem hiding this comment.
Ruby っぽくないのかもしれませんが、
return -1 if fewest_num_coins[amount] == Float::INFINITY # not existed
fewest_num_coins[amount]くらいのほうが素直ではないでしょうか。
Owner
Author
There was a problem hiding this comment.
確かにコメントで補足するという手もありですね。ありがとうございます。
potrue
reviewed
Sep 29, 2025
| fewest_num_coins[0] = 0 | ||
| 1.upto(amount).each do |sub_amount| | ||
| coins.each do |coin| | ||
| next if sub_amount - coin < 0 |
There was a problem hiding this comment.
どちらでもよいと思いますが、0から順にsub_amountを見ていってsub_amount + coin <= amountの場合にsub_amount + coinの場所を更新していく方法もあると思いました。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
解いた問題
322. Coin Change
使用言語
Ruby
次に解く問題
35. Search Insert Position