-
Notifications
You must be signed in to change notification settings - Fork 0
Subarray Sum Equals K.md #15
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
```python | ||
class Solution: | ||
def subarraySum(self, nums: List[int], k: int) -> int: | ||
accumulating_list = [0] |
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.
list という単語は型から分かるため、変数名に読み手にとって有益な情報を追加していないように思います。 accumulated あたりはいかがでしょうか?
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のスタイルガイドにも以下のように書かれています。
Omit types and type-like words from most variable names.
https://google.github.io/styleguide/go/decisions#variable-names
|
||
- 人のコードをみた後に改めて自分のコードを見ると、変数名`occurrence`が明らかに異質に見える。 | ||
- 変数名を決めた時の気持ちは「ここまでみてきた中で特定の数が発生した個数」と言いたかった。 | ||
- 「XXの個数」という意味はcountという用語で統一した方がわかりやすそう? |
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.
頻度を表す frequency を使う方もいらっしゃいます。チームの平均的なメンバーが、最速で正確に認知負荷少なく理解できる単語を使うことをお勧めいたします。
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.
個人的にはfrequencyの方が好きです
def subarraySum(self, nums: List[int], k: int) -> int: | ||
prefix_sum = 0 | ||
prefix_sum_to_count = defaultdict(int) | ||
prefix_sum_to_count[0] = 1 |
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.
prefix_sum_to_count[0] = 1
prefix_sum_to_count[prefix_sum] += 1
は、形式的な操作として、ループの先頭に移動させてまとめようと思えばまとめられますね。
意味としては分かりにくい気がしますが。
``` | ||
|
||
- 書いた後の感想。 | ||
- 結構resultを使いたくなることがわかった。 |
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.
result を使うというのも状況次第ではよいと思っています。
多くの場合関数名はしっかりと説明的になっているので、そうならば、「ここに完成品を構築します」という意図がはっきりし、上から読んでいったときに意図が追いやすいこともあるからです。ただ、それは上から読んでいったときに中間状態がどのようなものであるかが推測できる時でしょう。
一回、読み手の気持ちになってみましょう。
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.
別のプルリクであった議論を共有しておきます
t0hsumi/leetcode#13 (comment)
num_subarrays = 0 | ||
for num in nums: | ||
prefix_sum += num | ||
num_subarrays += prefix_sum_to_count[prefix_sum - k] |
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.
下でもresultを結局使っていますが、個人的にもそちらの方が好きです。(LeetCodeの関数名が微妙なので、もうちょっと関数名をしっかりすること前提ですが)
num_subarraysだと、「和がkのものに限る」というのがわかりにくく感じます。(部分列全部の数かな?と思ってしまう)
class Solution: | ||
def subarraySum(self, nums: List[int], k: int) -> int: | ||
prefix_sum = 0 | ||
prefix_sum_to_count = defaultdict(int) |
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.
もしやられたことがないなら、defaultdictを使わずdictだけ使うならどうなるか、余裕があるならdefaultdictのCPython実装がどうかを少し見てみると勉強になるかもしれません。
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.
レビュー依頼見落としてました。良いと思います。自分ならまず最初は愚直にすべてのSubarrayについて和を求めてみてそれがkと等しくなるか確認するかなと思います。
return result | ||
``` | ||
|
||
2重ループではTLEしたのでO(n)で解決するように修正。 |
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.
nの取りうる最大が2 * 10^4でこれくらいならC++ならたぶんstep1の解放でもTLEしない気がします。
https://leetcode.com/problems/subarray-sum-equals-k/description/