-
Notifications
You must be signed in to change notification settings - Fork 0
39. Combination Sum #52
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| ## 何も見ずに解いてみる | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
| vector<vector<int>> result; | ||
| vector<int> initial_combination; | ||
| combinationSumHelper(candidates, initial_combination, target, result); | ||
| return result; | ||
| } | ||
| private: | ||
| void combinationSumHelper(vector<int>& candidates, vector<int> combination, int target, vector<vector<int>>& result) { | ||
| if (target == 0) { | ||
| result.emplace_back(combination); | ||
| return; | ||
| } | ||
| if (candidates.empty()) { | ||
| return; | ||
| } | ||
| int num = candidates.back(); | ||
| candidates.pop_back(); | ||
| while (target >= 0) { | ||
| combinationSumHelper(candidates, combination, target, result); | ||
| combination.emplace_back(num); | ||
| target -= num; | ||
| } | ||
| candidates.emplace_back(num); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| ## 他の人のコードを見てみる | ||
|
|
||
| https://github.com/tokuhirat/LeetCode/pull/52/files | ||
| https://github.com/ryosuketc/leetcode_arai60/pull/41/files | ||
| https://github.com/Ryotaro25/leetcode_first60/pull/56/files | ||
|
|
||
| 最初に書いたコードはcombinationが値渡しであったことで結構時間がかかっていたようです。 | ||
| 以下のようにするだけでleetcode上の実行時間が20msぐらいから1msぐらいになりました。 | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
| vector<vector<int>> result; | ||
| vector<int> combination; | ||
| combinationSumHelper(candidates, combination, target, result); | ||
| return result; | ||
| } | ||
| private: | ||
| void combinationSumHelper(vector<int>& candidates, vector<int>& combination, int target, vector<vector<int>>& result) { | ||
| if (target == 0) { | ||
| result.emplace_back(combination); | ||
| return; | ||
| } | ||
| if (candidates.empty()) { | ||
| return; | ||
| } | ||
| int num = candidates.back(); | ||
| candidates.pop_back(); | ||
| while (target >= 0) { | ||
| combinationSumHelper(candidates, combination, target, result); | ||
| combination.emplace_back(num); | ||
| target -= num; | ||
| } | ||
| candidates.emplace_back(num); | ||
| while (!combination.empty() && combination.back() == num) { | ||
| combination.pop_back(); | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 大きい数字から見ていったほうが若干高速ではありそうですが、出力の形がきれいじゃなくなってしまいますね。 | ||
|
|
||
| ## 最終コード | ||
|
|
||
| ```cpp | ||
| class Solution { | ||
| public: | ||
| vector<vector<int>> combinationSum(vector<int>& candidates, int target) { | ||
| vector<vector<int>> result; | ||
| vector<int> initial_combination; | ||
| combinationSumHelper(candidates, initial_combination, 0, target, result); | ||
| return result; | ||
| } | ||
| private: | ||
| void combinationSumHelper( | ||
| const vector<int>& candidates, | ||
| vector<int>& combination, | ||
| int processed_until, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. processed_until は candidates のインデックスなので、candidates の直後に書きたいです。また好みではありますが、start ぐらいでも伝わるかなと思いました。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 説明的ではあると思うのですが、 |
||
| int target, | ||
| vector<vector<int>>& result | ||
| ) { | ||
| if (target < 0) { | ||
| return; | ||
| } | ||
| if (target == 0) { | ||
| result.emplace_back(combination); | ||
| return; | ||
| } | ||
| for (int i = processed_until; i < candidates.size(); ++i) { | ||
| combination.emplace_back(candidates[i]); | ||
| combinationSumHelper(candidates, combination, i, target - candidates[i], result); | ||
| combination.pop_back(); | ||
| } | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| 最後のiという変数名にはもう少し意味を持たせたいと思ったのですが、あまり簡潔な変数名が思いつかず・・・ | ||
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.
while (target >= 0) が回る回数を数えておき、以下のようにした方が直接理解しやすいと思いました。