Skip to content

Commit

Permalink
Create zero-filled-subarrays.py
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedonnan authored Mar 21, 2023
1 parent cc5159c commit f6fccaa
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions zero-filled-subarrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def zeroFilledSubarray(self, nums: List[int]) -> int:
flag = False
total = 0
for i in range(len(nums)):
if not flag and nums[i] == 0:
flag = True
startLoc = i
if flag and nums[i] != 0:
flag = False
total += self.addTotal(i - startLoc)
if flag:
total += self.addTotal(len(nums) - startLoc)
return total

@staticmethod
def addTotal(length: int) -> int:
length += 1
return int((length * (length-1)) / 2)

0 comments on commit f6fccaa

Please sign in to comment.