Conversation
| return all_combinations; | ||
| } | ||
| private: | ||
| struct CombinationAndTargetAndStart { |
There was a problem hiding this comment.
名前がいまいちな気がしますね。Personの代わりに、FirstNameAndLastNameAndAgeみたいな名前をつけるような印象です。CombinationContextとかSearchStateでどうでしょうか?
| state.push({{}, target, 0}); | ||
|
|
||
| while (!state.empty()) { | ||
| auto [combination, remaining, start] = state.top(); |
| for (int i = start; i < candidates.size(); ++i) { | ||
| std::vector<int> next_combination = combination; | ||
| next_combination.push_back(candidates[i]); | ||
| state.push({next_combination, remaining - candidates[i], i}); |
| const std::vector<int>& candidates, | ||
| int target, | ||
| int start, | ||
| std::vector<int>& current_combination, | ||
| std::vector<std::vector<int>>& all_combinations |
There was a problem hiding this comment.
引数がやや多いように感じました。その内3個は毎回同じなので、lambdaを使うか、変わらない部分はstructでまとめるとかあたりですかね。
| Dynamic programming でも解けるみたい。 | ||
|
|
||
| この考えはなかった。この問題の制約上あんまり劇的には効率化できないが、発想としては大事だなあ。 | ||
| > 先にcandidatesをsortしておけば、total+candidates[i] > target となったときにi以降の要素を見る必要はないのでサボれる。 |
There was a problem hiding this comment.
322. Coin Change でも同じような最適化がありましたね。
| return all_combinations; | ||
| } | ||
| private: | ||
| void findCombinations( |
There was a problem hiding this comment.
LeetCode の関数名と同じように命名していることかと思いますが、昔 Google C++ Style Guide を参照されていたような気がするので、それに従うなら PascalCase が良いですかね。
https://google.github.io/styleguide/cppguide.html#Function_Names
Ordinarily, functions follow PascalCase: start with a capital letter and have a capital letter for each new word.
AddTableEntry()
DeleteUrl()
OpenFileOrDie()
| class Solution { | ||
| public: | ||
| std::vector<std::vector<int>> combinationSum(std::vector<int>& candidates, int target) { | ||
| std::vector<std::vector<int>> all_combinations; |
There was a problem hiding this comment.
すぐに解決策が思いつかないのですが、current_combination は処理のためのバッファで呼び出し元は本来知らなくていいのに、呼び出し元で作成し渡してやらなくてはいけないのがちょっとモヤッとしました。
|
コメントされている箇所以外は、概ね読みやすかったです。 |
問題
https://leetcode.com/problems/combination-sum/
次の問題
22. Generate Parentheses