LeetCode Entry
2348. Number of Zero-Filled Subarrays
Count 0-subarrays
2348. Number of Zero-Filled Subarrays medium blog post substack youtube

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1086
Problem TLDR
Count 0-subarrays #medium #counting
Intuition
Count zero islands, use arithmetic sum: n(n+1)/2
Approach
- instead of arithmetics, just add current count to the result
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
// 5ms
fun zeroFilledSubarray(n: IntArray) =
n.fold(0L) { r, t ->
if (t == 0) ++n[0] else n[0] = 0; r + n[0]
}
// 3ms
fun zeroFilledSubarray(n: IntArray): Long {
var res = 0L; var curr = 0
for (x in n) {
if (x == 0) ++curr else curr = 0
res += curr
}
return res
}
// 0ms
pub fn zero_filled_subarray(n: Vec<i32>) -> i64 {
let (mut r, mut c) = (0, 0);
for x in n { if (x == 0) { c += 1 } else { c = 0 }; r += c } r
}
// 0ms
long long zeroFilledSubarray(vector<int>& n) {
long long r = 0;
for (int c = 0; int x: n) r += c = x ? 0: ++c;
return r;
}
// 49ms
zeroFilledSubarray = lambda _,n: sum(accumulate(n, lambda c,x: (c+1)*(x==0), initial=0))