Longest Zero Run Between Ones
Problem statement
Given an array bits of zeros and ones, find the longest block of consecutive zeros that has a 1 directly before it and a 1 directly after it.
Return its length, or 0 if no such block exists. Zeros at the very start or the very end of the array are not enclosed, because one side has no 1.
Function
longestEnclosedZeros(bits: int[]) → intExamples
Example 1
bits = [0,1,0,0,1,0,1,0,0,0,0]return = 2Enclosed runs: two zeros between the 1s at positions 1 and 4, and one zero between positions 4 and 6. The four zeros at the end have no 1 after them, so they do not count. The answer is 2.
Example 2
bits = [1,1,1]return = 0There are no zeros, so the answer is 0.
Example 3
bits = [0,0,1,0,0,0,1,0]return = 3The leading two zeros and the last zero are not enclosed. The three zeros between the two 1s are: the answer is 3.
Constraints
1 <= bits.length <= 20000- each
bits[i]is0or1