Conversation
| @@ -0,0 +1,17 @@ | |||
| sys.setrecursionlimit(2000) | |||
There was a problem hiding this comment.
これはどういう意図で追加していますか?どうして2000ですか?
(単なる興味です)
There was a problem hiding this comment.
参考にしていた書籍が2000でやってたので、意図は全く無いです。
| sys.setrecursionlimit(2000) | ||
| class Solution: | ||
| def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: | ||
| def make_combinations(remain, combination, begin): |
There was a problem hiding this comment.
なんとなくbeginの順番がもう少し左側な気がします。重要な引数ほど左にいると良いのかなと思っていてremain, begin, combinationの順番の方が良いかなと思います。個人的にはbegin, remain, combinationですが、ここまでいくと好みの問題かもしれないです。
重要な引数ほど左にいると良いというのは、Pythonなど言語によっては引数のデフォルト値も取れることも関係していると思います。
There was a problem hiding this comment.
重要な引数ほど左にいると良い
このことを知らなかったので、順番は意識してませんでした。
ありがとうございます。
| if remain == 0: | ||
| combinations.append(combination[:]) | ||
| return | ||
| for i in range(begin, len(candidates)): |
There was a problem hiding this comment.
begin == len(candidates)のときにrangeで空のシーケンスが変えるので結果的に終了しますが、明示的にチェックして上げても良いかなと思います。
| return | ||
| for i in range(begin, len(candidates)): | ||
| combination.append(candidates[i]) | ||
| make_combinations(remain - candidates[i], combination, i) |
There was a problem hiding this comment.
個人的には合計がtargetになるために必要な残りの値(remain)を管理するより、現在の合計を管理する方が素直かなと思いますが、好みの問題かもしれないです。
There was a problem hiding this comment.
現在の合計だと次に何が来ればいいのかすぐ分からず、結局target-現在値を考えるのかと思いremainにしました。
確かに素直な実装じゃない気がしてきました。
問題
https://leetcode.com/problems/combination-sum/