Skip to content

Create 560. Subarray Sum Equals K.md#33

Merged
Mike0121 merged 1 commit intomainfrom
560.-Subarray-Sum-Equals-K
Nov 1, 2025
Merged

Create 560. Subarray Sum Equals K.md#33
Mike0121 merged 1 commit intomainfrom
560.-Subarray-Sum-Equals-K

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@tshimosake tshimosake Jun 23, 2024

Choose a reason for hiding this comment

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

defaultdict を使っているので、この2行は単に(if 文は省略して) subarray_count += prefix_sum_to_count[prefix_sum - k] と書けます。

追記:「この2行」とは

if prefix_sum - k in prefix_sum_to_count:
    subarray_count += prefix_sum_to_count[prefix_sum - k]

のことです。github上だとどこを指しているのかわかりにくかったので追記しました🙏

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.

@tshimosake さん、 @SuperHotDogCat さん
ありがとうございます。if文確かに不要でした。

@SuperHotDogCat
Copy link
Copy Markdown

上でも言われているようにdefaultdictなのでif文はいらないかなと思います。それ以外は良いと思います。

@TORUS0818
Copy link
Copy Markdown

因みにDPの解法(TLE)もあります。
hayashi-ay/leetcode#31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいですが、from collections import defaultdictから書いてあると、親切かなと思います。
(レビューの際、コードを実行して確認したりするケースもあるので)

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.

ありがとうございます、そこは考慮できていませんでした。できるだけ書く様にします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@Mike0121 Mike0121 merged commit f06d085 into main Nov 1, 2025
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.

5 participants