Conversation
tokuhirat
reviewed
Oct 5, 2025
Comment on lines
+116
to
+123
| max_usage = (target - total) / candidates[index] | ||
| (1..max_usage).each do |i| | ||
| new_total = total + candidates[index] * i | ||
| i.times { partial_combination << candidates[index] } | ||
| combination_sum_helper.call(index + 1, new_total, partial_combination) | ||
| i.times { partial_combination.pop } | ||
| end | ||
| combination_sum_helper.call(index + 1, total, partial_combination) |
There was a problem hiding this comment.
ループで毎回 partial_combination へ追加と削除しているのが気になりました。せっかく max_usage を求めているので、ループ内で一つ追加して、ループを抜けた時に max_usage 回 pop したら簡潔になると思いました。candidates[index] を 0 回使うとしてまとめる書き方もあるかもしれません。
combination_sum_helper.call(index + 1, total, partial_combination)
max_usage = (target - total) / candidates[index]
(1..max_usage).each do |i|
total += candidates[index]
partial_combination << candidates[index]
combination_sum_helper.call(index + 1, total, partial_combination)
end
max_usage.times { partial_combination.pop }
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます!
こちらの方が簡潔で良いですね。
# @param {Integer[]} candidates
# @param {Integer} target
# @return {Integer[][]}
def combination_sum(candidates, target)
combinations = []
combination_sum_helper = lambda do |index, total, partial_combination|
if total == target
combinations << partial_combination.dup
return
end
return unless index < candidates.size
combination_sum_helper.call(index + 1, total, partial_combination)
max_usage = (target - total) / candidates[index]
(1..max_usage).each do |i|
total += candidates[index]
partial_combination << candidates[index]
combination_sum_helper.call(index + 1, total, partial_combination)
end
max_usage.times { partial_combination.pop }
end
combination_sum_helper.call(0, 0, [])
combinations
end
potrue
reviewed
Oct 5, 2025
| end | ||
| end | ||
| combination_sum_helper.call(0, 0, []) | ||
| combinations |
There was a problem hiding this comment.
いいと思います。個人的にはB を一つ使うか、C 以降しか使ってはいけないか、に分岐するやり方が一番読みやすいと思いました。
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.
解いた問題
39. Combination Sum
使用言語
Ruby
次に解く問題
https://leetcode.com/problems/generate-parentheses/description/