Skip to content

560. Subarray Sum Equals K #30

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

Merged
merged 2 commits into from
Feb 1, 2025
Merged

560. Subarray Sum Equals K #30

merged 2 commits into from
Feb 1, 2025

Conversation

@@ -0,0 +1,22 @@
/*
Time : O(N)

Choose a reason for hiding this comment

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

NlogNじゃないですか?mapへの要素の追加はlogNでそN回ループを回るので

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます。
見落としてました、私のミスです

Choose a reason for hiding this comment

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

unordered_mapを使えば定数時間で要素追加できるんじゃないでしょうか

Choose a reason for hiding this comment

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

定数倍の部分が大事なので、定数時間で要素追加できるとしてもunordered_mapの方が優れているとは限らないです

https://chromium.googlesource.com/chromium/src/+/master/base/containers/README.md#std_unordered_map-and-std_unordered_set

Do not default to using std::unordered_set and std::unordered_map. In the common case, query performance is unlikely to be sufficiently higher than std::map to make a difference, insert performance is slightly worse, and the memory overhead is high. This makes sense mostly for large tables where you expect a lot of lookups.

Choose a reason for hiding this comment

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

なるほど 要件によって性能変わってくるんですね
ありがとうございます

class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
vector<int> cumulative_nums = {0};
Copy link

Choose a reason for hiding this comment

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

std::exclusive_scan() を使ってもよいかもしれません。
https://cpprefjp.github.io/reference/numeric/exclusive_scan.html

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、これは知らなかったです、ありがとうございます。

Comment on lines +18 to +21
for (int i = cumulative_nums.size() - 1; i > 0; --i) {
cumulative_num_to_count[cumulative_nums[i]]++;
subarray_count += cumulative_num_to_count[k + cumulative_nums[i - 1]];
}
Copy link

Choose a reason for hiding this comment

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

後ろからでも前からでもいいですが、これのややこしいところは、cumulative_nums[i - 1] が何なのかの意味と記号が遠いからのように思います。ある程度は慣れですが、コメントを書いたり、変数に一回おいたりすると整理されるかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

なるほど、ありがとうございます。

cumulative_nums[i - 1]だけ取り出すとちょっと意味が分かりづらくなってしまっていますね。
書いているときは明確だった気がするのですが、今見るとかなり分かりづらいです。
このあたりの整理をうまいことできるようにしたいです。

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