30:00
Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum equal to goal.
nums
goal
A subarray is a contiguous part of the array.
1≤nums.length≤3×1041 \leq \text{nums.length} \leq 3 \times 10^41≤nums.length≤3×104
nums[i]\text{nums}[i]nums[i] is either 000 or 111
0≤goal≤nums.length0 \leq \text{goal} \leq \text{nums.length}0≤goal≤nums.length
Run code to see results.
For each right end, the starts that give sum 2 form one band [lo, hi). Two left pointers track its edges.
goal = 2
0
1
2
3
4
sum from lo
-
sum from hi
band hi - lo
count
Given a binary array and a target sum, count the non-empty contiguous subarrays whose elements add up to exactly that target.