diff options
Diffstat (limited to 'num_zero_filled_subarr.c')
-rw-r--r-- | num_zero_filled_subarr.c | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/num_zero_filled_subarr.c b/num_zero_filled_subarr.c index b8fd33d..8b7e23e 100644 --- a/num_zero_filled_subarr.c +++ b/num_zero_filled_subarr.c @@ -1,17 +1,19 @@ // 2348. Number of Zero-Filled Subarrays -#include <stdio.h> +#include "leetcode.h" // given integer array 'nums', return number of subarrays filled with zero. a // subarray is a contiguous non-empty sequence of elements from within an array -long long zeroFilledSubarray(int *nums, int nums_size) { - long long ans = 0; - for (int i = 0, j = 0; i < nums_size; ++i) { - if (nums[i] != 0) - j = i + 1; - ans += i - j + 1; +long long zeroFilledSubarray(int *nums, int numsSize) { + long long cnt = 0, streak = 0; + for (int i = 0; i < numsSize; i++) { + if (!nums[i]) { + streak++; + cnt += streak; + } else + streak = 0; } - return ans; + return cnt; } int main() { |