Skip to content

Latest commit

 

History

History
49 lines (31 loc) · 960 Bytes

_930. Binary Subarrays With Sum.md

File metadata and controls

49 lines (31 loc) · 960 Bytes

First completed : June 21, 2024

Last updated : June 21, 2024


Related Topics : Array, Hash Table, Sliding Window, Prefix Sum

Acceptance Rate : 62.625 %


To see the question prompt, click the title.

Solutions

Python

# Extremly similar to m1248

class Solution:
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        output = 0
        runningSum = 0

        prefixes = defaultdict(int)
        prefixes[0] = 1

        for num in nums :
            runningSum += num

            difference = runningSum - goal

            if difference in prefixes :
                output += prefixes[difference]
            
            prefixes[runningSum] += 1

        return output