-
Notifications
You must be signed in to change notification settings - Fork 0
Create 560. Subarray Sum Equals K.md #12
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
Open
mt2324
wants to merge
1
commit into
main
Choose a base branch
from
560.-Subarray-Sum-Equals-K
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| ## STEP 1 | ||
| ```python | ||
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| count = 0 | ||
| cumulative_sums = {0:1} | ||
| cumulative_sum = 0 | ||
| for num in nums: | ||
| cumulative_sum += num | ||
| count += cumulative_sums.get(cumulative_sum - k, 0) | ||
| cumulative_sums[cumulative_sum] = cumulative_sums.get(cumulative_sum, 0) + 1 | ||
| return count | ||
| ``` | ||
|
|
||
| これだとタイムアウトした。 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1 <= nums.length <= 2 * 104 であり、 dict が CPython でハッシュテーブルで実装されていることを考えると、タイムアウトすることは考えられないと思います。実際、 LeetCode に投げたところ、 28 ms でした。 Yuto729/leetcode#16 |
||
| 普通のdictだとgetが重いらしい。defaultdictを使うと良さそう。 | ||
|
|
||
| ## STEP 2 | ||
| 累積和はcumulative sumでもいいんだけどスペルに気を取られるのでprefix sumの方が楽。 | ||
| ```python | ||
| from collections import defaultdict | ||
|
|
||
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| sum_k_count = 0 | ||
| prefix_sum = 0 | ||
| prefix_sum_to_count = defaultdict(int) | ||
| prefix_sum_to_count[0] = 1 | ||
| for num in nums: | ||
| prefix_sum += num | ||
| sum_k_count += prefix_sum_to_count[prefix_sum - k] | ||
| prefix_sum_to_count[prefix_sum] += 1 | ||
| return sum_k_count | ||
| ``` | ||
| 実行時間の見積もりをやったことがなかったのでやってみるPythonだと安全寄りに見積って100 万ステップ/秒で。この場合最大データ20000なのでO(n)だと0.02秒、O(n^2)だと400秒なので厳しい。lognだとどうなるんだろうと思ったが2^10 が大体1000であることを考えると2^20 は大体1000,000,2^30 は1000,000,000になることがわかってれば大体計算できそう。 | ||
| [Yuto729/leetcode#16](https://github.com/Yuto729/leetcode/pull/16) | ||
|
|
||
| 全部正の値だとsliding window使えて、必要な空間計算量が下がるんですが今回は無理ですねというのが類似の問題との比較。 | ||
|
|
||
| ## STEP 3 | ||
| 練習。 | ||
| ```python | ||
| from collections import defaultdict | ||
|
|
||
| class Solution: | ||
| def subarraySum(self, nums: List[int], k: int) -> int: | ||
| prefix_sum = 0 | ||
| prefix_sums_count = defaultdict(int) | ||
| count = 0 | ||
| prefix_sums_count[0] = 1 | ||
| for num in nums: | ||
| prefix_sum += num | ||
| count += prefix_sums_count[prefix_sum - k] | ||
| prefix_sums_count[prefix_sum] += 1 | ||
| return count | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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://google.github.io/styleguide/pyguide.html#36-whitespace
なお、このスタイルガイドは“唯一の正解”というわけではなく、数あるガイドラインの一つに過ぎません。チームによって重視される書き方や慣習も異なります。そのため、ご自身の中に基準を持ちつつも、最終的にはチームの一般的な書き方に合わせることをお勧めします。