Skip to content

560. Subarray Sm Equals K#15

Open
maeken4 wants to merge 1 commit into
mainfrom
560.-Subarray-Sum-Equals-K
Open

560. Subarray Sm Equals K#15
maeken4 wants to merge 1 commit into
mainfrom
560.-Subarray-Sum-Equals-K

Conversation

@maeken4
Copy link
Copy Markdown
Owner

@maeken4 maeken4 commented Aug 2, 2025

class Solution {
public:
int subarraySum(const std::vector<int>& nums, int k) {
std::map<int, int> imterim_accumulate_count;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

imterim は interim の typo でしょうか。

accumulate が動詞のためか、キーと値に何が含まれるか、やや分かりずらく感じました。 prefix_sum_to_frequency や cumulative_sum_to_frequency はいかがでしょうか?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

失礼しました。おっしゃる通りinterimのタイポです…

prefix sumやcumulative sumで累積和の意味があるのを知りませんでした。これらを使ってみます。

int subarraySum(const std::vector<int>& nums, int k) {
std::map<int, int> imterim_accumulate_count;
int ans = 0;
int accumulate = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数名に動詞の原型を付けるのはあまり見ないように思います。 prefix_sum や cumulative_sum はいかがでしょうか?

std::map<int, int> imterim_accumulate_count;
int ans = 0;
int accumulate = 0;
for (const auto& num : nums) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

参照で受けると、間接アドレッシングで値にアクセスするコードが生成される場合があり、無駄な処理が行われる場合があります。レジスターサイズと比べて大きくない値については、値型で受け取ることをお勧めいたします。

詳しくは「Effective C++ 第3版」の「20項 値渡しよりconst参照渡しを使おう」をご覧ください。 (自分は第 2 版しか読んでおらず、この項の次の項に値渡しを使うことが書かれていました。第 3 版はこの項に値渡しを使う場合のことが書かれているのではないかと予想します。)

こちらもご覧ください。
Ryotaro25/leetcode_first60#44 (comment)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。手元にあるので確認してみました。レジスターサイズがどれくらいなのかよくわかっていなかったのですが、基本的に組み込み型は入るのですね。(64-bitPCなら64bit)

int subarraySum(const std::vector<int>& nums, int k) {
int result = 0;
int accumulate = 0;
std::map<int, int> accumulate_count = {{accumulate, 1}};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的にはハッシュマップに名前をつけるときはaccumulate_to_countのように間にtoを入れる方がわかりやすくて好きです。
そして、accumulateは動詞なので、cumulative sumやprefix sumの方がより良さそうです。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。たしかにここにaccumulate(動詞)が入るのは不自然な命名ですね。提案頂いた命名も使ってみます。

result += accumulate_count[accumulate - k];
accumulate_count[accumulate]++;
}
return result;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

resultという変数名はあまり情報量がないので、何が入っているのかがわかる名前をつける方が好きです。
今回だと num_subarrays_sum_equals_to_knum_subarrays でしょうか。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

ありがとうございます。たしかにそもそもメソッド名もあまりわかりやすくないのもあって、途中経過なのかわかりにくい面もありますね。

int result = 0;
int accumulate = 0;
std::map<int, int> accumulate_count = {{accumulate, 1}};
for (const auto& num : nums) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

intはお大きいデータサイズでは無いのでconst参照にしなくてもいいという意見もございます。

### その他
本問にはほぼ関係ないけどunordered_mapの効率化バージョン?であるflat_hash_mapというのがabseil?にあるらしい
https://hackmd.io/@elkurin/BJgKkoii3
Googleの人は標準ライブラリを作ってしまうのですごい…
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

社内の名前は ABC standard library だったかしら。
言語仕様が大きいので、すべてを許容すると管理ができなくなるので一部を切り出して使っています。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

戦略的に作らないと大変そうですね。そう考えると実際に仕様を決めたりgccを開発している人たちはいったい何者なんだ…という気持ちになりました。

Copy link
Copy Markdown

@ryosuketc ryosuketc left a comment

Choose a reason for hiding this comment

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

すでにコメントがついている通り、変数名や const 参照に関して見直すとよいかなと思いました。それ以外は特にコメントありませんでした。

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.

6 participants