<p>Given an integer array <code>nums</code>, return <em>the number of <strong>subarrays</strong> filled with </em><code>0</code>.</p>

<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 
There are 4 occurrences of [0] as a subarray.
There are 2 occurrences of [0,0] as a subarray.
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0,2,0,0]
<strong>Output:</strong> 9
<strong>Explanation:
</strong>There are 5 occurrences of [0] as a subarray.
There are 3 occurrences of [0,0] as a subarray.
There is 1 occurrence of [0,0,0] as a subarray.
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,10,2019]
<strong>Output:</strong> 0
<strong>Explanation:</strong> There is no subarray filled with 0. Therefore, we return 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def zeroFilledSubarray(self, nums: List[int]) -> int:
        # 000 --> 0, 0, 0, 00, 00, 000

        #  1 2 3
        #  0 0 0    --> 1, 2, 3, 12, 23, 123

        #  1 2 3 4
        #  0 0 0 0   --> 1, 2, 3, 4, 12, 23, 34, 123, 234, 1234

        # 1 zero --> 1, 2 zeros --> 3, 3 zeros --> 6, 4 zeros --> 10

        # n zeros --> 1 + 2 + 3 + 4 ... + n --> n(n+1)/2

        # Find lengths of zero-filled contigious arrays

        # output += 2(2+1)/2 = 0 + 3 = 3
        # output += 2(2+1)/2 = 3 + 3 = 6

        if(len(nums) == 0):
            return 0

        zeroStart = 0
        output = 0

        for zeroEnd in range(len(nums)):
            if(nums[zeroEnd] == 0 and (zeroEnd == len(nums) - 1 or nums[zeroEnd + 1] != 0)):
                # lengthOfSegment = zeroEnd - zeroStart + 1
                # numZeroSubarrays = lengthOfSegment * (lengthOfSegment + 1) // 2
                output += (((zeroEnd - zeroStart + 1) * (zeroEnd - zeroStart + 2)) // 2)
                # output += numZeroSubarrays
            elif(nums[zeroEnd] != 0 and (zeroEnd == len(nums) - 1 or nums[zeroEnd + 1] == 0)):
                zeroStart = zeroEnd + 1
        
        return output
            
        # # Test cases
        # input1 = [1,3,0,0,2,0,0,4]
        # print(zeroFilledSubarray(input1))

        # input2 = []
        # print(zeroFilledSubarray(input2))

        # input3 = [1,3,5,3,2,1,2,4]
        # print(zeroFilledSubarray(input3))

        # input4 = [0,0,0,0,0,0]
        # print(zeroFilledSubarray(input4))

        # input5 = [-1,0,-5,0,0,-3]
        # print(zeroFilledSubarray(input5))

        # input6 = [1, 0, 2, 0, 3, 0, 4, 0]
        # print(zeroFilledSubarray(input6))

        # input7 = [0, 2, 0, 3, 0, 4, 0, 1]
        # print(zeroFilledSubarray(input7))
        



        
       
798