-
Notifications
You must be signed in to change notification settings - Fork 0
39. Combination Sum #49
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
- subset は全部で 2^n 通り | ||
- candidates[i]=1 としたら、candidates[i] を 0~target 個含む場合が考えられる(target 通り) | ||
- なので時間計算量は O(target^n)? | ||
- 分割数で抑えられるらしい(https://discord.com/channels/1084280443945353267/1233295449985650688/1242067855579545611) |
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.
分割数の計算が絡む問題は、時々ありますが、他の人が解いているのは見たことがないですね。
- なので時間計算量は O(target^n)? | ||
- 分割数で抑えられるらしい(https://discord.com/channels/1084280443945353267/1233295449985650688/1242067855579545611) | ||
- 空間計算量: O(target * target^n)? | ||
- combination の長さが 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.
空間計算量が時間計算量を上回ることはありません。
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.
そうでした。
定数時間でアクセスできるメモリ領域は定数以下だからですね。
combination, sum, lastAddedIndex := top.combination, top.sum, top.lastAddedIndex | ||
stack = stack[:len(stack)-1] | ||
for i := lastAddedIndex; i < len(candidates); i++ { | ||
can := candidates[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.
この後、sum + can が何度か出てくるので、これを新しい変数にしてしまってもいいかと思いました。(下はそうしてますね。)
} | ||
``` | ||
|
||
### Step 3 |
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.
良さそうです。特に迷いなく書けているのではないでしょうか。
lastAddedIndex int | ||
} | ||
stack := []combinationBuilder{} | ||
for i, can := range candidates { |
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.
can が何を表すか初見で分かりにくく感じました。 candidate とフルスペルで書いたほうがよいかもしれません。
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.
ありがとうございます。フルスペルで書こうか迷ったのですが、candidateとcandidatesを区別するのも読み手の負担になるかもと思い略しました。dogとdogsならぱっと見でわかりますがcandidateと書くとむしろdidate辺りが邪魔になるのではと思いました。
この辺りソフトウェアエンジニアの常識としてはどうですか?
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.
あくまで個人的な感覚となりますが、 candidate は比較的よく使う単語のように思います。よく使う単語であれば、フルスペルで書かれても認知負荷は低いと思います。
また、別の言語のコーディングガイドラインとなりますが、英単語から文字を削って略語にするのは避けたほうがよいとしているものがあります。
https://google.github.io/styleguide/pyguide.html#316-naming
Avoid abbreviation. In particular, do not use abbreviations that are ambiguous or unfamiliar to readers outside your project, and do not abbreviate by deleting letters within a word.
https://google.github.io/styleguide/cppguide.html#General_Naming_Rules
Do not abbreviate by deleting letters within a word.
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.
ありがとうございます。Goのスタイルガイドを確認したところ、下記のような記載がありました。
Abbreviations can be acceptable loop identifiers when the scope is short, for example for _, n := range nodes { ... }.
https://google.github.io/styleguide/go/decisions#single-letter-variable-names
一方、
Limit their use to instances where the full word is obvious and where it would be repetitive for it to appear in place of the single-letter variable.
ともあるので、can が candidate の略であると明らかかというとそうではないですね。上記出典は1文字の変数名についての記載なので、複数文字の略語だと基準が甘くなるかもしれません。
https://leetcode.com/problems/combination-sum/description/