Skip to content

Conversation

@nittoco
Copy link
Owner

@nittoco nittoco commented Jul 10, 2024

https://leetcode.com/problems/combination-sum/description/

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency
of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

https://leetcode.com/problems/combination-sum/description/

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the 
frequency
 of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
```python
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
all_nums_and_sum = []

Choose a reason for hiding this comment

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

(Step1のものにレビューするのも少しあれですが...)numsよりかはcombinationsでしょうか, 問題文や関数にあるものを命名に使うといい感じになる気がします。

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
stack= [([], 0, 0)]
sum_is_target = []

Choose a reason for hiding this comment

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

step2のall_combinationsの方が個人的に好みです。sum_is_targetだと僕は英語のSVC構文のように, 見えてしまいます。

stack= [([], 0, 0)]
sum_is_target = []
while stack:
nums_so_far, index, sum_so_far = stack.pop()

Choose a reason for hiding this comment

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

nums_so_farcombinationcombination_nums にすると、何の数字の集まりなのかがより明確になる気がします。


class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
stack= [([], 0, 0)]

Choose a reason for hiding this comment

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

細かいですが、stack= がくっついていますね。

stack= [([], 0, 0)]
sum_is_target = []
while stack:
nums_so_far, index, sum_so_far = stack.pop()

Choose a reason for hiding this comment

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

これも細かくてすみませんが、=stack.pop() の間がスペース2つですね。

if combination_sum == target:
combination_target_sum.append(combination_searched.copy())
return
make_combination(index + 1, combination_sum)

Choose a reason for hiding this comment

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

組み合わせになり得るかの計算までに都度 複数回の再帰が必要で、動きを追いかけることが個人的に難しいと感じました。
この make_combination() は forループで書くこともできそうに思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。

この make_combination() は forループで書くこともできそうに思いました。

これは再帰を使わないで書く、ということですか。いまいち方法が思いつかず、、、

Choose a reason for hiding this comment

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

ありがとうございます。

再帰を使わないで書く

はい、下記のようなイメージでした。

def make_combination(start, combination_sum):
    for index in range(start, len(candidates)):
        if combination_sum > target:
            return
        if combination_sum == target:
            combination_target_sum.append(combination_searched.copy())
            return
        combination_searched.append(candidates[index])
        make_combination(index, combination_sum + candidates[index])
        combination_searched.pop()

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、理解しました。コードまでありがとうございます。
確かに2回再帰をやってappendしたりpopしたりの操作をするとわかりにくいかもですね。

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants