Skip to content

Commit

Permalink
Add solution for Binary Subarrays With Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacknahil committed Feb 8, 2024
1 parent 4d0097c commit 88f8f68
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions week9/leetcode/binary-subarrays-with-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
def atmost(nums,goal):
left=0
count=0
cur=0
for right in range(len(nums)):
cur+=nums[right]
while cur>goal and left<=right:
cur-=nums[left]
left+=1
count+=right-left+1
return count
return atmost(nums,goal)-atmost(nums,goal-1)

0 comments on commit 88f8f68

Please sign in to comment.