Skip to content

39. Combination Sum - #46

Open
akmhmgc wants to merge 1 commit into
mainfrom
39
Open

39. Combination Sum#46
akmhmgc wants to merge 1 commit into
mainfrom
39

Conversation

@akmhmgc

@akmhmgc akmhmgc commented Oct 5, 2025

Copy link
Copy Markdown
Owner

解いた問題

39. Combination Sum

使用言語

Ruby

次に解く問題

https://leetcode.com/problems/generate-parentheses/description/

@akmhmgc akmhmgc added the ruby label Oct 5, 2025
Comment thread 39/step2.md
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ループで毎回 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 }

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.

コメントありがとうございます!
こちらの方が簡潔で良いですね。

# @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

Comment thread 39/step3.md
end
end
combination_sum_helper.call(0, 0, [])
combinations

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

いいと思います。個人的にはB を一つ使うか、C 以降しか使ってはいけないか、に分岐するやり方が一番読みやすいと思いました。

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