Skip to content

560. Subarray Sum Equals K - #16

Open
kazizi55 wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k
Open

560. Subarray Sum Equals K#16
kazizi55 wants to merge 1 commit into
mainfrom
560-subarray-sum-equals-k

Conversation

@kazizi55

Copy link
Copy Markdown
Owner

## Step 1

- 与えられた nums から合計が k と一致する subarray (1つのみも含む)を作成して返すというもの。
- nums を最初から探索していって、累積和を足していく、k と一致したら答えを increment して累積和を0にする、k を超えたら累積和を0に戻すのだけする、という形でいけそう。

@h-masder h-masder Jul 30, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

仮にnumsの要素がすべて正の数だったとしても、このやり方ですと、カウントする部分配列が重なっているとき取りこぼすと思います。
例えば、nums = [1, 1, 1, 1, 1], k = 3のときに部分配列nums[1:4] (nums[1] + nums[2] + nums[3]がkになる)をカウントできなさそうです。

正の数だったら、スライディングウィンドウが使えるかなと思いました。

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.

ありがとうございます。

このやり方ですと、カウントする部分配列が重なっているとき取りこぼすと思います。

確かにそうですね。

正の数だったら、スライディングウィンドウが使えるかなと思いました。

確かにこのように書けますね。単調増加か減少でないと window を縮めるロジックが破綻する (縮める結果が一意でなくなる) ため、入力が正の数か負の数のみである必要があるのですね。

def subarraySum(nums: list[int], k: int) -> int:
    left = 0
    prefix_sum = 0
    num_k_subarrays = 0
    
    for right in range(len(nums)):
        prefix_sum += nums[right]
        
        while prefix_sum > k and left <= right:
            prefix_sum -= nums[left]
            left += 1
            
        if prefix_sum == k:
            num_k_subarrays += 1
            
    return num_k_subarrays

return k_subarray_total
```

- まず、arai60 の hash map の欄にあったのに hashmap を使った解法を考慮できていなかった。反省。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

個人的には、そこまでカテゴリにこだわらなくていいと思います。
それよりもhashmapのつかいどころをおさえておくといいかなと思いました。
(あるリストを線形探索する回数が多い、かつ、リストが長いときにhash mapで検索したほうが早い場合がある、など。リストが短く1回しか探索しない場合、ハッシュ計算をするくらいなら線形探索したほうが早いこともあります)

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.

ありがとうございます。
確かに使い所を押さえてこそのカテゴリですね。
この問題も例えば nums の総数が数個程度だったら線形探索の方が早そうですね。


- まず、arai60 の hash map の欄にあったのに hashmap を使った解法を考慮できていなかった。反省。
- gemini に聞きながら書き直し。
- 書いてみてもしっくりこなかったのだが、oda さんの標高差の例をもとに図を書いてみたらかなり腑に落ちた。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

例を書くのいいと思います

def subarraySum(self, nums: List[int], k: int) -> int:
k_subarray_total = 0
current_total = 0
prefix_total_to_frequency = defaultdict(int)

@h-masder h-masder Jul 30, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

current_totalとprefix_totalは同じものだと思うので、統一するのはいかがでしょうか。
また、累積和はprefix_sumでいいのかなとも思いました。

        prefix_sum = 0
        prefix_sum_to_frequency  = defaultdict(int)

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.

おっしゃる通りですね。

step2 以降は統一して書いております 🙏


## Step 4


Copy link
Copy Markdown

Choose a reason for hiding this comment

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

全体的にとても読みやすいと思いました

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.

2 participants