Skip to content

560. Subarray Sum Equals K#16

Open
seal-azarashi wants to merge 9 commits intomainfrom
subarray-sum-equals-k
Open

560. Subarray Sum Equals K#16
seal-azarashi wants to merge 9 commits intomainfrom
subarray-sum-equals-k

Conversation

@seal-azarashi
Copy link
Copy Markdown
Owner

@Yoshiki-Iwasa
Copy link
Copy Markdown

良さそうです!


// sum(nums[0:j]) - k = sum(nums[0:i]) となる i (0 <= i < j) が x 個存在すれば
// k と等しい部分配列 sum(nums[i:j]) も x 個存在する
count += cumulativeSumFrequency.getOrDefault(cumulativeSum - k, 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.

感想: cumulativeSumCountでもいいのかなと。countに向けて加算してますし

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.

確かに、全然 ~Count でも意味通りますね

- 累積和の出現回数を格納する map はそのまま cumulativeSumFrequency, 累積和を格納する整数値は cumulativeSum とした
- 最近 Go のコードをよく読んでいたので冗長に感じる気持ちがないでもないが、まあ Java なのでこれにした
- あとはなるべく素直に実装したつもり
- 61-63 行は `count += cumulativeSumFrequency.getOrDefault(cumulativeSum - k, 0);` とも書けるが、これだとこの関数の処理の詳細に加えて getOrDefault() の挙動の両方をちゃんと把握していないと内容の理解が出来ないので、若干認知負荷が高い気がしたので今回はこのようにしてみた
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には認知負荷が高まるとは感じませんでした

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.

ありがとうございます!結局次のステップで getOrDefault() を使い一行で書くように修正したのですが、認知負荷高まらないとのことで安心しました。

@nittoco
Copy link
Copy Markdown

nittoco commented Aug 8, 2024

みました!良さそうです!

@TORUS0818
Copy link
Copy Markdown

拝見しました。
良さそうです。

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.

4 participants