From 7c60dee83b436e8e8a667bf9f191beced8de4fc3 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Mon, 10 Mar 2025 00:36:13 -0600 Subject: chore: move cpp and rust code to directory --- other/num_zero_filled_subarr.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 other/num_zero_filled_subarr.cpp (limited to 'other/num_zero_filled_subarr.cpp') diff --git a/other/num_zero_filled_subarr.cpp b/other/num_zero_filled_subarr.cpp new file mode 100644 index 0000000..8ba45f3 --- /dev/null +++ b/other/num_zero_filled_subarr.cpp @@ -0,0 +1,29 @@ +// 2348. Number of Zero-Filled Subarrays +#include "leetcode.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 + +class Solution { +public: + long long zeroFilledSubarray(vector &nums) { + long long ans = 0; + short n = nums.size(); + for (int i = 0, j = 0; i < n; ++i) { + if (nums[i] != 0) + j = i + 1; + ans += i - j + 1; + } + return ans; + } +}; + +int main() { + Solution obj; + vector n1 = {1, 3, 0, 0, 2, 0, 0, 4}; + vector n2 = {0, 0, 0, 2, 0, 0}; + vector n3 = {2, 10, 2019}; + printf("%lld\n", obj.zeroFilledSubarray(n1)); // expect: 6 + printf("%lld\n", obj.zeroFilledSubarray(n2)); // expect: 9 + printf("%lld\n", obj.zeroFilledSubarray(n3)); // expect: 0 +} -- cgit v1.2.3