subarray_sum_equals_k

 1from collections import defaultdict
 2
 3
 4# @leet start
 5class Solution:
 6    def subarraySum(self, nums: list[int], k: int) -> int:
 7        """
 8        Given an array and an integer, k, return the number of subarrays that
 9        have a sum of k.
10
11        To find this out, we can find the total sum up to this point, i.
12        If we find the same i, we know that the sum of indexes between
13        the two points is 0. Thus, we've found another subarray that adds
14        to k, and we need to increment the count of subarrays equal k by
15        the amount of times we've seen the same `number - k`.
16        """
17        count, total = 0, 0
18        sums = defaultdict(int)
19        sums[0] = 1
20        for num in nums:
21            total += num
22            count += sums[total - k]
23            sums[total] += 1
24        return count
25
26
27# @leet end
28
29
30def test():
31    assert 2 + 2 == 4
class Solution:
 6class Solution:
 7    def subarraySum(self, nums: list[int], k: int) -> int:
 8        """
 9        Given an array and an integer, k, return the number of subarrays that
10        have a sum of k.
11
12        To find this out, we can find the total sum up to this point, i.
13        If we find the same i, we know that the sum of indexes between
14        the two points is 0. Thus, we've found another subarray that adds
15        to k, and we need to increment the count of subarrays equal k by
16        the amount of times we've seen the same `number - k`.
17        """
18        count, total = 0, 0
19        sums = defaultdict(int)
20        sums[0] = 1
21        for num in nums:
22            total += num
23            count += sums[total - k]
24            sums[total] += 1
25        return count
def subarraySum(self, nums: list[int], k: int) -> int:
 7    def subarraySum(self, nums: list[int], k: int) -> int:
 8        """
 9        Given an array and an integer, k, return the number of subarrays that
10        have a sum of k.
11
12        To find this out, we can find the total sum up to this point, i.
13        If we find the same i, we know that the sum of indexes between
14        the two points is 0. Thus, we've found another subarray that adds
15        to k, and we need to increment the count of subarrays equal k by
16        the amount of times we've seen the same `number - k`.
17        """
18        count, total = 0, 0
19        sums = defaultdict(int)
20        sums[0] = 1
21        for num in nums:
22            total += num
23            count += sums[total - k]
24            sums[total] += 1
25        return count

Given an array and an integer, k, return the number of subarrays that have a sum of k.

To find this out, we can find the total sum up to this point, i. If we find the same i, we know that the sum of indexes between the two points is 0. Thus, we've found another subarray that adds to k, and we need to increment the count of subarrays equal k by the amount of times we've seen the same number - k.

def test():
31def test():
32    assert 2 + 2 == 4