add: 230321

This commit is contained in:
Eatswap 2023-03-21 23:54:03 +08:00
parent 5fb0da597d
commit 0bc79d6395
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 35 additions and 1 deletions

34
cpp/2303/230321.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <vector>
#include <iostream>
/**
* 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<int>& 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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2303)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2303 230321-CN.cpp) ADD_EXECUTABLE(2303 230321.cpp)