-
Notifications
You must be signed in to change notification settings - Fork 0
Add 560. Subarray Sum Equals K.md #17
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
# step 2 | ||
- https://discord.com/channels/1084280443945353267/1233603535862628432/1252232545056063548 | ||
- 駅と標高を使って、累積和を高低差みたいに捉えている。 | ||
- わかりやすい。今いる地点から標高差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.
はじめから [0] = 1 であることがしっくり来ていたらこの話は問題ないと思います。
https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0
subarrays_count += cumulative_sum_to_count.get( | ||
cumulative_sum - k, | ||
0 | ||
) | ||
cumulative_sum_to_count[cumulative_sum] += 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.
cumulative_sum_to_countにdefaultdictを使う場合、シンプルに
subarrays_count += cumulative_sum_to_count[cumulative_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.
それをすると、cumulative_sum_to_count[cumulative_sum - k] = 0が追加されて不要なメモリ使用が増えそうだから避けようくらいの考えでした。
よくよく考えれば、最悪の場合の空間計算量は変わらないので、提案していただいたものでも問題ありませんね
sub_sum = nums[left] | ||
for right in range(left, len(nums)): | ||
if right != left: | ||
sub_sum += nums[right] |
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.
sub_sum = 0
から始めれば、if right != left:
のチェックなしでsub_sum
に追加していけます。
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.
確かにそうですね。ありがとうございます!
def subarraySum(self, nums: List[int], k: int) -> int: | ||
cumulative_sum = 0 | ||
cumulative_sum_to_count = defaultdict(int) | ||
cumulative_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.
この行にコメントを付けたいと思いました。
初見だと、なぜnums
を走査していないのに累積和がすでにカウントされているのか?、と思うかもしれません。
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.
個人的には、L88-L91までは初期化の塊と認識していて、コメントの必要は感じていませんでした。
「走査を始める前の、何も数えていない状態の累積和もいれるんだな」くらいのイメージです。
ただ、そのイメージを伝え切れるコードではなかったかもしれません。
cumulative_sum_to_count[cumulative_sum] += 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.
つけるコメントを考えてみたのですが、なかなかいいものが思いつかなかったので、やっぱりなくてもいいかもしれません。
https://leetcode.com/problems/subarray-sum-equals-k/description/