[LeetCode] Number of Zero-Filled Subarrays

2348. Number of Zero-Filled Subarrays

Given an integer array nums, return the number of subarrays filled with 0.

A subarray is a contiguous non-empty sequence of elements within an array.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
long long zeroFilledSubarray(vector<int>& nums) {
long long res = 0;
for(int i = 0, cnt = 0; i < nums.size(); i++) {
if(nums[i]) cnt = 0;
else cnt += 1;
res += cnt;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/07/24/PS/LeetCode/number-of-zero-filled-subarrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.