Skip to content

Commit f79efd3

Browse files
authored
Create ways-to-split-array-into-good-subarrays.cpp
1 parent 495a241 commit f79efd3

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// combinatorics
5+
class Solution {
6+
public:
7+
int numberOfGoodSubarraySplits(vector<int>& nums) {
8+
static const int MOD = 1e9 + 7;
9+
int result = 1, prev = -1;
10+
for (int i = 0; i < size(nums); ++i) {
11+
if (nums[i] != 1) {
12+
continue;
13+
}
14+
if (prev != -1) {
15+
result = (result * static_cast<int64_t>(i - prev)) % MOD;
16+
}
17+
prev = i;
18+
}
19+
return prev != -1 ? result : 0;
20+
}
21+
};

0 commit comments

Comments
 (0)