Skip to content

Conversation

hroc135
Copy link
Owner

@hroc135 hroc135 commented Apr 19, 2025

- subset は全部で 2^n 通り
- candidates[i]=1 としたら、candidates[i] を 0~target 個含む場合が考えられる(target 通り)
- なので時間計算量は O(target^n)?
- 分割数で抑えられるらしい(https://discord.com/channels/1084280443945353267/1233295449985650688/1242067855579545611)
Copy link

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 になりうるので
Copy link

Choose a reason for hiding this comment

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

空間計算量が時間計算量を上回ることはありません。

Copy link
Owner Author

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]
Copy link

Choose a reason for hiding this comment

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

この後、sum + can が何度か出てくるので、これを新しい変数にしてしまってもいいかと思いました。(下はそうしてますね。)

}
```

### Step 3
Copy link

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 {
Copy link

Choose a reason for hiding this comment

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

can が何を表すか初見で分かりにくく感じました。 candidate とフルスペルで書いたほうがよいかもしれません。

Copy link
Owner Author

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辺りが邪魔になるのではと思いました。
この辺りソフトウェアエンジニアの常識としてはどうですか?

Copy link

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.

Copy link
Owner Author

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文字の変数名についての記載なので、複数文字の略語だと基準が甘くなるかもしれません。

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.

3 participants