Skip to content

Commit 3fbd1f6

Browse files
authored
Create 0930-binary-subarrays-with-sum.py
1 parent 0ab4587 commit 3fbd1f6

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
3+
4+
def helper(x):
5+
if x < 0: return 0
6+
7+
res = 0
8+
l, cur = 0, 0
9+
for r in range(len(nums)):
10+
cur += nums[r]
11+
while cur > x:
12+
cur -= nums[l]
13+
l += 1
14+
res += (r - l + 1)
15+
return res
16+
17+
return helper(goal) - helper(goal - 1)

0 commit comments

Comments
 (0)