Skip to content

Latest commit

 

History

History
51 lines (32 loc) · 1.12 KB

_930. Binary Subarrays With Sum.md

File metadata and controls

51 lines (32 loc) · 1.12 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 21, 2024

Last updated : June 21, 2024


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

Acceptance Rate : 62.625 %


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