-
Notifications
You must be signed in to change notification settings - Fork 0
322. Coin Change #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
322. Coin Change #35
Conversation
| 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not_existedは変数に置かないでいいと思います。置くとしてもこの行の前でいいと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
コメントありがとうございます!
定数におきたいのですがRubyはメソッドの中で定数を定義できないので変数にしています。
(メソッドの外には定義できるが、スコープが広くなるのでやりたくない)
一方で、おっしゃる通り場所は直前で良いかもしれませんね。
There was a problem hiding this comment.
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]くらいのほうが素直ではないでしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
確かにコメントで補足するという手もありですね。ありがとうございます。
| 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.
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の場所を更新していく方法もあると思いました。
解いた問題
322. Coin Change
使用言語
Ruby
次に解く問題
35. Search Insert Position