Skip to content

Commit 42e7675

Browse files
authored
feat: add solutions to lc problem: No.2750 (doocs#1077)
No.2750.Ways to Split Array Into Good Subarrays
1 parent 7355f18 commit 42e7675

File tree

8 files changed

+306
-6
lines changed

8 files changed

+306
-6
lines changed

solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README.md

+109-3
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,140 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:乘法原理**
51+
52+
根据题目描述,我们可以在两个 $1$ 之间画一条分割线,假设两个 $1$ 之间的下标分别为 $j$ 和 $i$,那么可以画的不同分割线的数量为 $i - j$。我们找出所有满足条件的 $j$ 和 $i$,然后将所有的 $i - j$ 相乘即可。如果找不到两个 $1$ 之间的分割线,那么说明数组中不存在 $1$,此时答案为 $0$。
53+
54+
时间复杂度 $O(n)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
<!-- 这里可写当前语言的特殊实现逻辑 -->
5561

5662
```python
57-
63+
class Solution:
64+
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
65+
mod = 10**9 + 7
66+
ans, j = 1, -1
67+
for i, x in enumerate(nums):
68+
if x == 0:
69+
continue
70+
if j > -1:
71+
ans = ans * (i - j) % mod
72+
j = i
73+
return 0 if j == -1 else ans
5874
```
5975

6076
### **Java**
6177

6278
<!-- 这里可写当前语言的特殊实现逻辑 -->
6379

6480
```java
65-
81+
class Solution {
82+
public int numberOfGoodSubarraySplits(int[] nums) {
83+
final int mod = (int) 1e9 + 7;
84+
int ans = 1, j = -1;
85+
for (int i = 0; i < nums.length; ++i) {
86+
if (nums[i] == 0) {
87+
continue;
88+
}
89+
if (j > -1) {
90+
ans = (int) ((long) ans * (i - j) % mod);
91+
}
92+
j = i;
93+
}
94+
return j == -1 ? 0 : ans;
95+
}
96+
}
6697
```
6798

6899
### **C++**
69100

70101
```cpp
71-
102+
class Solution {
103+
public:
104+
int numberOfGoodSubarraySplits(vector<int>& nums) {
105+
const int mod = 1e9 + 7;
106+
int ans = 1, j = -1;
107+
for (int i = 0; i < nums.size(); ++i) {
108+
if (nums[i] == 0) {
109+
continue;
110+
}
111+
if (j > -1) {
112+
ans = 1LL * ans * (i - j) % mod;
113+
}
114+
j = i;
115+
}
116+
return j == -1 ? 0 : ans;
117+
}
118+
};
72119
```
73120
74121
### **Go**
75122
76123
```go
124+
func numberOfGoodSubarraySplits(nums []int) int {
125+
const mod int = 1e9 + 7
126+
ans, j := 1, -1
127+
for i, x := range nums {
128+
if x == 0 {
129+
continue
130+
}
131+
if j > -1 {
132+
ans = ans * (i - j) % mod
133+
}
134+
j = i
135+
}
136+
if j == -1 {
137+
return 0
138+
}
139+
return ans
140+
}
141+
```
142+
143+
### **TypeScript**
144+
145+
```ts
146+
function numberOfGoodSubarraySplits(nums: number[]): number {
147+
let ans = 1;
148+
let j = -1;
149+
const mod = 10 ** 9 + 7;
150+
const n = nums.length;
151+
for (let i = 0; i < n; ++i) {
152+
if (nums[i] === 0) {
153+
continue;
154+
}
155+
if (j > -1) {
156+
ans = (ans * (i - j)) % mod;
157+
}
158+
j = i;
159+
}
160+
return j === -1 ? 0 : ans;
161+
}
162+
```
77163

164+
### **C#**
165+
166+
```cs
167+
public class Solution {
168+
public int NumberOfGoodSubarraySplits(int[] nums) {
169+
long ans = 1, j = -1;
170+
int mod = 1000000007;
171+
int n = nums.Length;
172+
for (int i = 0; i < n; ++i) {
173+
if (nums[i] == 0) {
174+
continue;
175+
}
176+
if (j > -1) {
177+
ans = ans * (i - j) % mod;
178+
}
179+
j = i;
180+
}
181+
return j == -1 ? 0 : (int) ans;
182+
}
183+
}
78184
```
79185

80186
### **...**

solution/2700-2799/2750.Ways to Split Array Into Good Subarrays/README_EN.md

+103-3
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,125 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
53+
mod = 10**9 + 7
54+
ans, j = 1, -1
55+
for i, x in enumerate(nums):
56+
if x == 0:
57+
continue
58+
if j > -1:
59+
ans = ans * (i - j) % mod
60+
j = i
61+
return 0 if j == -1 else ans
5262
```
5363

5464
### **Java**
5565

5666
```java
57-
67+
class Solution {
68+
public int numberOfGoodSubarraySplits(int[] nums) {
69+
final int mod = (int) 1e9 + 7;
70+
int ans = 1, j = -1;
71+
for (int i = 0; i < nums.length; ++i) {
72+
if (nums[i] == 0) {
73+
continue;
74+
}
75+
if (j > -1) {
76+
ans = (int) ((long) ans * (i - j) % mod);
77+
}
78+
j = i;
79+
}
80+
return j == -1 ? 0 : ans;
81+
}
82+
}
5883
```
5984

6085
### **C++**
6186

6287
```cpp
63-
88+
class Solution {
89+
public:
90+
int numberOfGoodSubarraySplits(vector<int>& nums) {
91+
const int mod = 1e9 + 7;
92+
int ans = 1, j = -1;
93+
for (int i = 0; i < nums.size(); ++i) {
94+
if (nums[i] == 0) {
95+
continue;
96+
}
97+
if (j > -1) {
98+
ans = 1LL * ans * (i - j) % mod;
99+
}
100+
j = i;
101+
}
102+
return j == -1 ? 0 : ans;
103+
}
104+
};
64105
```
65106
66107
### **Go**
67108
68109
```go
110+
func numberOfGoodSubarraySplits(nums []int) int {
111+
const mod int = 1e9 + 7
112+
ans, j := 1, -1
113+
for i, x := range nums {
114+
if x == 0 {
115+
continue
116+
}
117+
if j > -1 {
118+
ans = ans * (i - j) % mod
119+
}
120+
j = i
121+
}
122+
if j == -1 {
123+
return 0
124+
}
125+
return ans
126+
}
127+
```
128+
129+
### **TypeScript**
130+
131+
```ts
132+
function numberOfGoodSubarraySplits(nums: number[]): number {
133+
let ans = 1;
134+
let j = -1;
135+
const mod = 10 ** 9 + 7;
136+
const n = nums.length;
137+
for (let i = 0; i < n; ++i) {
138+
if (nums[i] === 0) {
139+
continue;
140+
}
141+
if (j > -1) {
142+
ans = (ans * (i - j)) % mod;
143+
}
144+
j = i;
145+
}
146+
return j === -1 ? 0 : ans;
147+
}
148+
```
69149

150+
### **C#**
151+
152+
```cs
153+
public class Solution {
154+
public int NumberOfGoodSubarraySplits(int[] nums) {
155+
long ans = 1, j = -1;
156+
int mod = 1000000007;
157+
int n = nums.Length;
158+
for (int i = 0; i < n; ++i) {
159+
if (nums[i] == 0) {
160+
continue;
161+
}
162+
if (j > -1) {
163+
ans = ans * (i - j) % mod;
164+
}
165+
j = i;
166+
}
167+
return j == -1 ? 0 : (int) ans;
168+
}
169+
}
70170
```
71171

72172
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int numberOfGoodSubarraySplits(vector<int>& nums) {
4+
const int mod = 1e9 + 7;
5+
int ans = 1, j = -1;
6+
for (int i = 0; i < nums.size(); ++i) {
7+
if (nums[i] == 0) {
8+
continue;
9+
}
10+
if (j > -1) {
11+
ans = 1LL * ans * (i - j) % mod;
12+
}
13+
j = i;
14+
}
15+
return j == -1 ? 0 : ans;
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public int NumberOfGoodSubarraySplits(int[] nums) {
3+
long ans = 1, j = -1;
4+
int mod = 1000000007;
5+
int n = nums.Length;
6+
for (int i = 0; i < n; ++i) {
7+
if (nums[i] == 0) {
8+
continue;
9+
}
10+
if (j > -1) {
11+
ans = ans * (i - j) % mod;
12+
}
13+
j = i;
14+
}
15+
return j == -1 ? 0 : (int) ans;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func numberOfGoodSubarraySplits(nums []int) int {
2+
const mod int = 1e9 + 7
3+
ans, j := 1, -1
4+
for i, x := range nums {
5+
if x == 0 {
6+
continue
7+
}
8+
if j > -1 {
9+
ans = ans * (i - j) % mod
10+
}
11+
j = i
12+
}
13+
if j == -1 {
14+
return 0
15+
}
16+
return ans
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int numberOfGoodSubarraySplits(int[] nums) {
3+
final int mod = (int) 1e9 + 7;
4+
int ans = 1, j = -1;
5+
for (int i = 0; i < nums.length; ++i) {
6+
if (nums[i] == 0) {
7+
continue;
8+
}
9+
if (j > -1) {
10+
ans = (int) ((long) ans * (i - j) % mod);
11+
}
12+
j = i;
13+
}
14+
return j == -1 ? 0 : ans;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def numberOfGoodSubarraySplits(self, nums: List[int]) -> int:
3+
mod = 10**9 + 7
4+
ans, j = 1, -1
5+
for i, x in enumerate(nums):
6+
if x == 0:
7+
continue
8+
if j > -1:
9+
ans = ans * (i - j) % mod
10+
j = i
11+
return 0 if j == -1 else ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function numberOfGoodSubarraySplits(nums: number[]): number {
2+
let ans = 1;
3+
let j = -1;
4+
const mod = 10 ** 9 + 7;
5+
const n = nums.length;
6+
for (let i = 0; i < n; ++i) {
7+
if (nums[i] === 0) {
8+
continue;
9+
}
10+
if (j > -1) {
11+
ans = (ans * (i - j)) % mod;
12+
}
13+
j = i;
14+
}
15+
return j === -1 ? 0 : ans;
16+
}

0 commit comments

Comments
 (0)