diff --git a/966-binary-subarrays-with-sum/binary-subarrays-with-sum.py b/966-binary-subarrays-with-sum/binary-subarrays-with-sum.py new file mode 100644 index 0000000..285efc2 --- /dev/null +++ b/966-binary-subarrays-with-sum/binary-subarrays-with-sum.py @@ -0,0 +1,16 @@ +class Solution: + def numSubarraysWithSum(self, nums: List[int], goal: int) -> int: + def helper(x): + if x < 0: return 0 + + res = 0 + l, cur = 0, 0 + for r in range(len(nums)): + cur += nums[r] + while cur > x: + cur -= nums[l] + l += 1 + res += (r - l + 1) + return res + + return helper(goal) - helper(goal - 1) \ No newline at end of file