math and geometry November 12, 2022

Number of zero filled subarrays

Time O(n) Space O(1) Open original problem

We will iterate over each number, if the number is zero, we will add the number of subarrays that can be formed with the previous numbers to the result. We will also add the number of subarrays that can be formed with the previous numbers to the result.

class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        res, count = 0, 0
        for num in nums:
            if num != 0:
                count = 0
            else:
                count += 1
            res += count
        return res

Time Complexity: O(n)
Space Complexity: O(1)