Open
Conversation
mamo3gr
reviewed
Feb 15, 2026
Comment on lines
+112
to
+118
| for _, num := range nums { | ||
| prefixSum += num | ||
| if c, found := prefixSumToCount[prefixSum - k]; found { | ||
| count += c | ||
| } | ||
| prefixSumToCount[prefixSum]++ | ||
| } |
There was a problem hiding this comment.
単なる感想として…
numまでのprefixSumを求めながら、それまで登場したprefixSum - kのカウントを足すことで部分配列を数え上げる、という2つのことを同じループで処理している- 2つの
countが登場して紛らわしい
あたりが、読解するのに苦労するなあと思いました。
※現状のコードから改善するポイントは特に思いつかないのですが
Owner
Author
There was a problem hiding this comment.
ありがとうございます。
改善するとしたら
countをsubarrayCountなど何の数え上げなのか明示する- コメントを書く
などが考えられるかなと思いました。
また時間をとって見直してみます。
There was a problem hiding this comment.
num までの prefixSum を求めながら、それまで登場した prefixSum - k のカウントを足すことで部分配列を数え上げる、という2つのことを同じループで処理している
一つの方法としては、
- prefixSum を求めるループ
- 和が k になる subarray の数を、prefixSum から求めるループ
の二つのループに分けてしまうというのもありなのかなと思いました。
せいぜい計算量も O(n) で 言語もGoなので、計算時間としても大体数ms 犠牲にするぐらいで済むと思います。
TakayaShirai
reviewed
Mar 5, 2026
Comment on lines
+112
to
+118
| for _, num := range nums { | ||
| prefixSum += num | ||
| if c, found := prefixSumToCount[prefixSum - k]; found { | ||
| count += c | ||
| } | ||
| prefixSumToCount[prefixSum]++ | ||
| } |
There was a problem hiding this comment.
num までの prefixSum を求めながら、それまで登場した prefixSum - k のカウントを足すことで部分配列を数え上げる、という2つのことを同じループで処理している
一つの方法としては、
- prefixSum を求めるループ
- 和が k になる subarray の数を、prefixSum から求めるループ
の二つのループに分けてしまうというのもありなのかなと思いました。
せいぜい計算量も O(n) で 言語もGoなので、計算時間としても大体数ms 犠牲にするぐらいで済むと思います。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
今回の問題
Subarray Sum Equals K - LeetCode
使用言語
Go
次に解く問題
Number of Islands - LeetCode