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.
# 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