From 0bc79d6395b31cda87c9e449d5c13290c23dc4c6 Mon Sep 17 00:00:00 2001 From: Eatswap Date: Tue, 21 Mar 2023 23:54:03 +0800 Subject: [PATCH] add: 230321 --- cpp/2303/230321.cpp | 34 ++++++++++++++++++++++++++++++++++ cpp/2303/CMakeLists.txt | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 cpp/2303/230321.cpp diff --git a/cpp/2303/230321.cpp b/cpp/2303/230321.cpp new file mode 100644 index 0000000..172b264 --- /dev/null +++ b/cpp/2303/230321.cpp @@ -0,0 +1,34 @@ +#include +#include + +/** + * 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. + */ + +class Solution { +private: + static constexpr long long f(long long x) { + return x * (x + 1) / 2; + } + +public: + static long long zeroFilledSubarray(const std::vector& n) { + long long ans = 0; + int c = 0; + for (int i : n) { + if (i) { + ans += f(c); + c = 0; + } else ++c; + } + return ans + f(c); + } +}; + +int main() { + std::cout << Solution::zeroFilledSubarray({2,10,2019}); + return 0; +} diff --git a/cpp/2303/CMakeLists.txt b/cpp/2303/CMakeLists.txt index a6a12de..ea59d31 100644 --- a/cpp/2303/CMakeLists.txt +++ b/cpp/2303/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2303) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2303 230321-CN.cpp) +ADD_EXECUTABLE(2303 230321.cpp)