diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /num_zero_filled_subarr.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'num_zero_filled_subarr.c')
-rw-r--r-- | num_zero_filled_subarr.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/num_zero_filled_subarr.c b/num_zero_filled_subarr.c new file mode 100644 index 0000000..b8fd33d --- /dev/null +++ b/num_zero_filled_subarr.c @@ -0,0 +1,24 @@ +// 2348. Number of Zero-Filled Subarrays +#include <stdio.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; + } + return ans; +} + +int main() { + int n1[] = {1, 3, 0, 0, 2, 0, 0, 4}; + int n2[] = {0, 0, 0, 2, 0, 0}; + int n3[] = {2, 10, 2019}; + printf("%lld\n", zeroFilledSubarray(n1, 8)); // expect: 6 + printf("%lld\n", zeroFilledSubarray(n2, 6)); // expect: 9 + printf("%lld\n", zeroFilledSubarray(n3, 3)); // expect: 0 +} |