Skip to content

Conversation

@Ryotaro25
Copy link
Owner

問題へのリンク
https://leetcode.com/problems/combination-sum/description/

問題文(プレミアムの場合)

備考

次に解く問題の予告
Generate Parentheses

フォルダ構成
LeetCodeの問題ごとにフォルダを作成します。
フォルダ内は、step1.cpp、step2.cpp、step3.cpp、loop.cppとmemo.mdとなります。

memo.md内に各ステップで感じたことを追記します。

Comment on lines +18 to +23
for (int i = start; i < candidates.size(); i++) {
vector<int> added_combination = partial_combination;
added_combination.push_back(candidates[i]);
combination_state.push({added_combination, remain - candidates[i], i});
}
}
Copy link

Choose a reason for hiding this comment

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

インデントずれていますね。

一つ上のぶら下がり文のせいでしょうか。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.gblcuhn8bpdi

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
レビューありがとうございます。該当箇所みました。
まさに事故を起こしておりました🙇

またこちらの纏めは60問解き終わってからきちんと目を通そうと思います。

Comment on lines +22 to +25
int partial_sum = 0;
for (int num : partial_candidate) {
partial_sum += num;
}
Copy link

Choose a reason for hiding this comment

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

これ、sum + candidates[i] ですか?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
sum + candidates[i] を使った方が素直ですね🙇‍♂️
再帰から戻ってきた際に、sum -= candidates[i];をしてsumで動くようにしました。

for (int i = start; i < candidates.size(); i++) {
        partial_candidate.push_back(candidates[i]);
        sum += candidates[i];
        GenerateCombinationToTarget(all_candidates, partial_candidate, candidates, target, i, sum);
        partial_candidate.pop_back();
        sum -= candidates[i];
      }

Copy link

Choose a reason for hiding this comment

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

あら、sum を更新せずに、新しい変数にするか引数に直接和を渡せば、引かなくてもいいのではないでしょうか。

Copy link
Owner Author

Choose a reason for hiding this comment

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

@oda
sumを足したり引いたりする方が、バックトラッキングぽいのかなと感じました。
実際は、回答になるpartial_candidateだけを増やしたり減らしたりすることかと思いますが。

public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> all_combinations;
stack<CombinationAndTargetAndIndex> combination_state;
Copy link

Choose a reason for hiding this comment

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

targetとremainって意味的に違う気がして、こっちの名称はremainですかね?
また、C++のstructの名称の付け方の慣習が自分よくわかってないのですが、3つの要素をただandで繋げるのはデータ定義見ればわかるよ、と思ってしまいがちなのですが、どうなんでしょう、、、?

Copy link
Owner Author

Choose a reason for hiding this comment

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

@nittoco
レビューありがとうございます。

3つの要素をただandで繋げるのはデータ定義見ればわかるよ、と思ってしまいがちなのですが、どうなんでしょう、、、?
慣習とかはないと思いあます🙇‍♂️このデータ構造を表すいい名前が思いつかずとりあえず入っているものを繋げました。

いっそのこと無理にstructを使わず、stackを3つ用いた方が分かりやすいですかね?

Copy link

Choose a reason for hiding this comment

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

うーん難しいですね、stackが3つなのもそれはそれで煩雑そうですし(はっきりしてなくてすみません)

struct CombinationAndTargetAndIndex {
vector<int> combination;
int remain;
int index;
Copy link

Choose a reason for hiding this comment

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

どういうindexかの情報が個人的には欲しいかもです(appending_indexとか?)

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