add: 230321
This commit is contained in:
parent
5fb0da597d
commit
0bc79d6395
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2303)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2303 230321-CN.cpp)
|
||||
ADD_EXECUTABLE(2303 230321.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue