Skip to content

Commit 007c6a6

Browse files
authored
Create 2348-number-of-zero-filled-subarrays.cpp
1 parent aa96c34 commit 007c6a6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Given an integer array nums, return the number of subarrays filled with 0.
3+
A subarray is a contiguous non-empty sequence of elements within an array.
4+
5+
Ex. Input: nums = [1,3,0,0,2,0,0,4]
6+
Output: 6
7+
Explanation:
8+
There are 4 occurrences of [0] as a subarray.
9+
There are 2 occurrences of [0,0] as a subarray.
10+
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.
11+
12+
Time : O(N)
13+
Space : O(1)
14+
*/
15+
16+
class Solution {
17+
public:
18+
long long zeroFilledSubarray(vector<int>& nums) {
19+
long long res = 0, count = 0;
20+
for (int i = 0; i < nums.size(); i++) {
21+
if (nums[i]) {
22+
res += (count * (count + 1)) / 2;
23+
count = 0;
24+
} else
25+
++count;
26+
}
27+
res += (count * (count + 1)) / 2;
28+
return res;
29+
}
30+
};

0 commit comments

Comments
 (0)