Medium
LABBinary Subarrays With Sum
Given a binary array, return the number of contiguous subarrays with sum equal to goal.
EXAMPLES
Example 1
Input
{
"nums": [
1,
0,
1,
0,
1
],
"goal": 2
}
Output
4FUNCTION SHAPE
nums: intArraygoal: int→intSOLUTION NOTE
Notice that this is actually just an easier case of Q560 Subarray Sum Equals K. The exact same code works here.
Reveal reference solution +
pythonREFERENCE
# This is just an easier case of Q560 - exact same code works!
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
return self.subarraySum(nums, goal)Time
O(n)Space
O(n)