Classic problem #2348MediumArrayMath
Number of Zero-Filled Subarrays
Real-world version: Stretches Holding Nothing but ZerosTracked in this browser only
Read Number of Zero-Filled Subarrays on LeetCode(opens in a new tab)
The statement here is our own: the same inputs, outputs and technique.
A log holds the readings readings.
Return how many stretches of one or more neighbouring readings hold nothing but zeros.
Examples
Example 1
- Input
- readings = [0, 0, 0]
- Output
- 6
Three single zeros, two neighbouring pairs and the whole run come to six stretches.
Example 2
- Input
- readings = [0, 1, 0]
- Output
- 2
Each zero stands on its own, so only the two one-reading stretches count.
Example 3
- Input
- readings = [0, 0, 1, 0, 0, 0]
- Output
- 9
The run of two zeros gives three stretches and the run of three gives six.
Constraints
- 1 <= readings.length <= 10^5
- -10^9 <= readings[i] <= 10^9
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
Python
def zero_filled_subarray(readings: list[int]) -> int:Java
public long zeroFilledSubarray(int[] readings)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's