-
Notifications
You must be signed in to change notification settings - Fork 0
39. Combination Sum #25
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?
Conversation
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 = [] |
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.
(Step1のものにレビューするのも少しあれですが...)numsよりかはcombinationsでしょうか, 問題文や関数にあるものを命名に使うといい感じになる気がします。
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| stack= [([], 0, 0)] | ||
| sum_is_target = [] |
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.
step2のall_combinationsの方が個人的に好みです。sum_is_targetだと僕は英語のSVC構文のように, 見えてしまいます。
| stack= [([], 0, 0)] | ||
| sum_is_target = [] | ||
| while stack: | ||
| nums_so_far, index, sum_so_far = stack.pop() |
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.
nums_so_far は combination や combination_nums にすると、何の数字の集まりなのかがより明確になる気がします。
|
|
||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| stack= [([], 0, 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.
細かいですが、stack= がくっついていますね。
| stack= [([], 0, 0)] | ||
| sum_is_target = [] | ||
| while stack: | ||
| nums_so_far, index, sum_so_far = stack.pop() |
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.
これも細かくてすみませんが、= と stack.pop() の間がスペース2つですね。
| if combination_sum == target: | ||
| combination_target_sum.append(combination_searched.copy()) | ||
| return | ||
| make_combination(index + 1, combination_sum) |
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.
組み合わせになり得るかの計算までに都度 複数回の再帰が必要で、動きを追いかけることが個人的に難しいと感じました。
この make_combination() は forループで書くこともできそうに思いました。
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.
ありがとうございます。
この
make_combination()は forループで書くこともできそうに思いました。
これは再帰を使わないで書く、ということですか。いまいち方法が思いつかず、、、
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.
ありがとうございます。
再帰を使わないで書く
はい、下記のようなイメージでした。
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()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.
なるほど、理解しました。コードまでありがとうございます。
確かに2回再帰をやってappendしたりpopしたりの操作をするとわかりにくいかもですね。
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.