All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 22, 2024
Last updated : June 22, 2024
Related Topics : Array, Hash Table, Sliding Window, Prefix Sum
Acceptance Rate : 63.31 %
# 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