-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2348-Number_of_Zero_Filled_Subarrays.cpp
91 lines (81 loc) · 2.2 KB
/
2348-Number_of_Zero_Filled_Subarrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*******************************************************************************
* 2348-Number_of_Zero_Filled_Subarrays.cpp
* Billy.Ljm
* 21 Mar 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/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.
*
* ===========
* My Approach
* ===========
* We can iterate through the array and count the number of consecutive 0's.
* Each consecutive block would then contain 1+2+..+n subarrrays which are all
* zero-filled.
*
* This would have a time complexity of O(n) and space complexity of O(1), where
* n is the length of the array.
******************************************************************************/
#include <iostream>
#include <vector>
class Solution {
public:
/**
* Count number of zero-filled subarrays
*
* @param nums array of integers
*
* @return number of zero-filled subarrays in nums
*/
long long zeroFilledSubarray(std::vector<int>& nums) {
long long total = 0LL;// total number of zero-filled subarrays
long long num0 = 0LL; // number of consecutive zero's
for (int i = 0; i < nums.size(); i++) {
if (nums[i] == 0) {
num0++;
}
else {
total += num0 * (num0 + 1) / 2; // 1+2+..+num0 arithmetic series
num0 = 0;
}
}
total += num0 * (num0 + 1) / 2; // trailing zeros
return total;
}
};
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
os << "[";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << ",";
}
os << "\b]";
return os;
}
/**
* Test cases
*/
int main(void) {
Solution sol;
std::vector<int> nums;
// test case 1
nums = { 1, 3, 0, 0, 2, 0, 0, 4 };
std::cout << "zeroFilledSubarray(" << nums << "): " <<
sol.zeroFilledSubarray(nums) << std::endl;
// test case 2
nums = { 0, 0, 0, 2, 0, 0 };
std::cout << "zeroFilledSubarray(" << nums << "): " <<
sol.zeroFilledSubarray(nums) << std::endl;
// test case 3
nums = { 2, 10, 2019 };
std::cout << "zeroFilledSubarray(" << nums << "): " <<
sol.zeroFilledSubarray(nums) << std::endl;
return 0;
}