Skip to content

Commit

Permalink
Time: 237 ms (12.61%) | Memory: 17.1 MB (99.51%) - LeetSync
Browse files Browse the repository at this point in the history
  • Loading branch information
ShatilKhan committed Mar 14, 2024
1 parent 08cc1ee commit 8ca71e1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 966-binary-subarrays-with-sum/binary-subarrays-with-sum.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8ca71e1

Please sign in to comment.