Skip to content

560. Subarray Sum Equals K#16

Open
rimokem wants to merge 1 commit into
mainfrom
0560-subarray-sum-equals-k
Open

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

Conversation

@rimokem
Copy link
Copy Markdown
Owner

@rimokem rimokem commented Apr 8, 2026

Comment thread 0560/memo.md
- `k`未満ならば、`right`を進める

という解法をまず思いついた。
しかし、これは`nums[i] > 0`が前提のため、今回は不適切。
Copy link
Copy Markdown

@h-masder h-masder Apr 8, 2026

Choose a reason for hiding this comment

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

なんとなくですが、この解法ができそうなのは、nums1がソートされているという前提も必要そうです。
いかがでしょうか。

ちょっと考えていたのですが、必要のは非負だけでした。すみません。。。

非負ではなく自然数でした。すみません...

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がソートされてなくても大丈夫なつもりで考えていました。

以下のコードが意図していたものです。

class Solution:
    def subarraySum(self, nums: List[int], k: int) -> int:
        left = 0
        partial_sum = 0
        count = 0

        for right in range(len(nums)):
            partial_sum += nums[right]

            while partial_sum >= k and left <= right:
                if partial_sum == k:
                    count += 1
                partial_sum -= nums[left]
                left += 1

        return count

nums[i]>0だとすると、

  • sum(nums[i:j]) > sum(nums[i:j]) - nums[i] == sum(nums[i+1:j])
  • sum(nums[i:j]) < sum(nums[i:j]) + nums[j+1] == sum(nums[i:j+1])

という関係が成り立ちます。
これを用いて、

  • sum(nums[left: right])) >= k ならばleftを増やす
  • sum(nums[left: right])) < k ならばrightを増やす

という方針で考えました。(時間O(n), 空間O(1))
ただ0以下の数が混じると上述の関係が崩れるので、今回は使えませんでした。

Copy link
Copy Markdown

@h-masder h-masder Apr 8, 2026

Choose a reason for hiding this comment

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

おっしゃる通りですね。丁寧に説明していただきありがとうございます。

・leftとrightの区間が k以上になるまでrightを進める。
・leftとrightの区間が k未満になるかleftがrightに追いつくまでleftを進める。
をやりながら、部分和がkになる回数を数えるのですね。

よくわかりました。ありがとうございます!

Copy link
Copy Markdown

@h-masder h-masder Apr 8, 2026

Choose a reason for hiding this comment

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

入力条件を変えながら、それに応じた解法をすぐに思い浮かべられるのは、とても良いですね。

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.

頭では考えていたのですが、実際には書いていなかったので良い機会になりました。

いつも丁寧なレビューありがとうございます。
今後もよろしくお願いします!

Comment thread 0560/memo.md
prefix_sum_count[current_sum] += 1

return count

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ふわっとした感想ですが、
step1の段階でこういったコードが書けるのは、とてもいいなと感じました。
解きかたとコードの対応がとりやすいです。

Comment thread 0560/memo.md
prefix_sum_count[0] = 1

count = 0
current_sum = 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここは、prefix_sumでもいいかなと思いました。

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