Skip to content

560. Subarray Sum Equals K #15

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions lc560.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# step1
思考ログ
- subarrayは空ではないと問題文にある
- subarrayだから、2 pointer, スライスを使って解けそう
- 負の数も考えないといけないからO(n^2)は避けて通れない?
- 愚直にやる方法だとtime limit exceededとでた(以下その実装)
```python
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
num_subarrays = 0

for i in range(len(nums)):
complement = k - nums[i]
if complement == 0:
num_subarrays += 1
for j in range(i+1, len(nums)):
Copy link

Choose a reason for hiding this comment

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

i + 1ですかね

complement -= nums[j]
if complement == 0:
num_subarrays += 1

return num_subarrays
```
- 1回計算した箇所は保存しておく方法でできるのではないか→実装がうまくいかなかったので断念答えを見る
```python
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
num_subarrays = 0
cumsum = 0
cumsum_to_count = defaultdict(int)
cumsum_to_count[0] = 1
for num in nums:
cumsum += num
if cumsum - k in cumsum_to_count:
num_subarrays += cumsum_to_count[cumsum - k]
cumsum_to_count[cumsum] += 1

return num_subarrays
```
# step2
参考にした方のPR
- https://github.com/Satorien/LeetCode/pull/16/files?short_path=567cd09#diff-567cd09815edd159a86e52be227aee58958dcad0ffe5c15ec6141af5b929797b
- "累積和はCountをHashMapに入れておく"というのがかなりしっくりきた

- https://github.com/potrue/leetcode/pull/16/files?short_path=9af6c29#diff-9af6c29cdcdd4f79ab8b011317151f6cd8b13f570a036eb4d7473388bc3f0d56
- chainを使って解いているがchainについて初めて見たので[ドキュメント]()https://docs.python.org/ja/3.13/library/itertools.html#itertools.chainを見る

Copy link

Choose a reason for hiding this comment

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

ドキュメントを読むのはいい癖だと思います。

それから読む先ですが完走者の中で気に入った人を見繕っておいてもいいかと思います。

- https://github.com/shintaroyoshida20/leetcode/pull/22/files#diff-5db2f0c29e6198a74e1b6caeaddee45eda91677924fcd859ee7060d0bfc08c85
c++の実装

- https://github.com/tokuhirat/LeetCode/pull/16/files#diff-d4900f989c6f9680b8e8144658ef8f10d6025523b2c0c63bed653dcdcc4fc290
- 変数について, resultを許容してもよい, countに対してfrequencyも候補である.

- https://github.com/katataku/leetcode/pull/15/files
- https://github.com/t0hsumi/leetcode/pull/13#discussion_r1902635530
ここら辺を見ていて, この[サイト](https://source.chromium.org/search?q=%22return%20res;%22%20filepath:.*%5C.cc$)を知った. 慣習的に使われている変数名とか検索できそう


# step 3
```python
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
num_subarrays = 0
cumsum = 0
cumsum_to_count = defaultdict(int)
cumsum_to_count[0] = 1
Copy link

Choose a reason for hiding this comment

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

多分好みの問題なのですが,自分はここはcumsum_to_count[cumsum] = 1が嬉しいです


for num in nums:
cumsum += num
if cumsum - k in cumsum_to_count:
Copy link

Choose a reason for hiding this comment

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

defaultdictを使うのであればこの条件分岐はなくてもいいかもしれません。
(defaultdictは評価された時点で辞書の中に値が入るので、条件分岐をしておくと無駄に辞書の中身が増えないという利点はありますが)

num_subarrays += cumsum_to_count[cumsum - k]
cumsum_to_count[cumsum] += 1

return num_subarrays
```

Choose a reason for hiding this comment

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

読みやすかったです。