-
Notifications
You must be signed in to change notification settings - Fork 0
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
Conversation
@@ -0,0 +1,22 @@ | |||
/* | |||
Time : 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.
NlogNじゃないですか?mapへの要素の追加はlogNでそ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.
コメントありがとうございます。
見落としてました、私のミスです
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.
unordered_mapを使えば定数時間で要素追加できるんじゃないでしょうか
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.
定数倍の部分が大事なので、定数時間で要素追加できるとしてもunordered_mapの方が優れているとは限らないです
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.
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.
なるほど 要件によって性能変わってくるんですね
ありがとうございます
class Solution { | ||
public: | ||
int subarraySum(vector<int>& nums, int k) { | ||
vector<int> cumulative_nums = {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.
std::exclusive_scan() を使ってもよいかもしれません。
https://cpprefjp.github.io/reference/numeric/exclusive_scan.html
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.
なるほど、これは知らなかったです、ありがとうございます。
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]]; | ||
} |
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_nums[i - 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_nums[i - 1]
だけ取り出すとちょっと意味が分かりづらくなってしまっていますね。
書いているときは明確だった気がするのですが、今見るとかなり分かりづらいです。
このあたりの整理をうまいことできるようにしたいです。
560. Subarray Sum Equals K
https://discord.com/channels/1084280443945353267/1233603535862628432/1252239510624931921
https://discord.com/channels/1084280443945353267/1183683738635346001/1192145962479665304
https://discord.com/channels/1084280443945353267/1183683738635346001/1192368274705498243
https://discord.com/channels/1084280443945353267/1195700948786491403/1254402602666430594
https://github.com/hayashi-ay/leetcode/pull/31/files
https://github.com/ryoooooory/LeetCode/pull/3/files
https://github.com/sakupan102/arai60-practice/pull/17/files
https://github.com/tshimosake/arai60/pull/7/files
https://github.com/Exzrgs/LeetCode/pull/25/files
https://github.com/fhiyo/leetcode/pull/19/files
https://github.com/SuperHotDogCat/coding-interview/pull/29/files
https://github.com/goto-untrapped/Arai60/pull/28/files
https://github.com/Ryotaro25/leetcode_first60/pull/17/files
https://github.com/TORUS0818/leetcode/pull/18/files
https://github.com/kazukiii/leetcode/pull/17/files
https://github.com/Mike0121/LeetCode/pull/33/files
https://github.com/Yoshiki-Iwasa/Arai60/pull/15/files
https://github.com/nittoco/leetcode/pull/22/files
https://github.com/seal-azarashi/leetcode/pull/16/files
https://github.com/hroc135/leetcode/pull/16/files
https://github.com/tarinaihitori/leetcode/pull/16/files