Skip to content

Commit 5f59e4a

Browse files
authored
feat: add solutions to lc problem: No.2871 (#1734)
No.2871.Split Array Into Maximum Number of Subarrays
1 parent e9cfffd commit 5f59e4a

File tree

6 files changed

+156
-8
lines changed

6 files changed

+156
-8
lines changed

solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,28 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
**方法一:贪心 + 位运算**
62+
63+
我们初始化一个变量 $score$,用来记录当前子数组的分数,初始时 $score = -1$。然后我们遍历数组,对于每个元素 $num$,我们将 $score$ 与 $num$ 进行按位与运算,然后将结果赋值给 $score$。如果 $score = 0$,说明当前子数组的分数为 $0$,我们就可以将当前子数组分割出来,然后将 $score$ 重置为 $-1$。最后我们返回分割出的子数组的个数。
64+
65+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
66+
6167
<!-- tabs:start -->
6268

6369
### **Python3**
6470

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

6773
```python
68-
74+
class Solution:
75+
def maxSubarrays(self, nums: List[int]) -> int:
76+
score, ans = -1, 1
77+
for num in nums:
78+
score &= num
79+
if score == 0:
80+
score = -1
81+
ans += 1
82+
return 1 if ans == 1 else ans - 1
6983
```
7084

7185
### **Java**
@@ -92,19 +106,55 @@ class Solution {
92106
### **C++**
93107

94108
```cpp
95-
109+
class Solution {
110+
public:
111+
int maxSubarrays(vector<int>& nums) {
112+
int score = -1, ans = 1;
113+
for (int num : nums) {
114+
score &= num;
115+
if (score == 0) {
116+
--score;
117+
++ans;
118+
}
119+
}
120+
return ans == 1 ? 1 : ans - 1;
121+
}
122+
};
96123
```
97124
98125
### **Go**
99126
100127
```go
101-
128+
func maxSubarrays(nums []int) int {
129+
ans, score := 1, -1
130+
for _, num := range nums {
131+
score &= num
132+
if score == 0 {
133+
score--
134+
ans++
135+
}
136+
}
137+
if ans == 1 {
138+
return 1
139+
}
140+
return ans - 1
141+
}
102142
```
103143

104144
### **TypeScript**
105145

106146
```ts
107-
147+
function maxSubarrays(nums: number[]): number {
148+
let [ans, score] = [1, -1];
149+
for (const num of nums) {
150+
score &= num;
151+
if (score === 0) {
152+
--score;
153+
++ans;
154+
}
155+
}
156+
return ans == 1 ? 1 : ans - 1;
157+
}
108158
```
109159

110160
### **...**

solution/2800-2899/2871.Split Array Into Maximum Number of Subarrays/README_EN.md

+54-4
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,26 @@ It can be shown that we cannot split the array into more than 1 subarray with a
5252

5353
## Solutions
5454

55+
**Solution 1: Greedy + Bitwise Operation**
56+
57+
We initialize a variable $score$ to record the score of the current subarray, and set $score=-1$ initially. Then we traverse the array, for each element $num$, we perform a bitwise AND operation between $score$ and $num$, and assign the result to $score$. If $score=0$, it means the score of the current subarray is 0, so we can split the current subarray and reset $score$ to $-1$. Finally, we return the number of split subarrays.
58+
59+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
60+
5561
<!-- tabs:start -->
5662

5763
### **Python3**
5864

5965
```python
60-
66+
class Solution:
67+
def maxSubarrays(self, nums: List[int]) -> int:
68+
score, ans = -1, 1
69+
for num in nums:
70+
score &= num
71+
if score == 0:
72+
score = -1
73+
ans += 1
74+
return 1 if ans == 1 else ans - 1
6175
```
6276

6377
### **Java**
@@ -82,19 +96,55 @@ class Solution {
8296
### **C++**
8397

8498
```cpp
85-
99+
class Solution {
100+
public:
101+
int maxSubarrays(vector<int>& nums) {
102+
int score = -1, ans = 1;
103+
for (int num : nums) {
104+
score &= num;
105+
if (score == 0) {
106+
--score;
107+
++ans;
108+
}
109+
}
110+
return ans == 1 ? 1 : ans - 1;
111+
}
112+
};
86113
```
87114
88115
### **Go**
89116
90117
```go
91-
118+
func maxSubarrays(nums []int) int {
119+
ans, score := 1, -1
120+
for _, num := range nums {
121+
score &= num
122+
if score == 0 {
123+
score--
124+
ans++
125+
}
126+
}
127+
if ans == 1 {
128+
return 1
129+
}
130+
return ans - 1
131+
}
92132
```
93133

94134
### **TypeScript**
95135

96136
```ts
97-
137+
function maxSubarrays(nums: number[]): number {
138+
let [ans, score] = [1, -1];
139+
for (const num of nums) {
140+
score &= num;
141+
if (score === 0) {
142+
--score;
143+
++ans;
144+
}
145+
}
146+
return ans == 1 ? 1 : ans - 1;
147+
}
98148
```
99149

100150
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int maxSubarrays(vector<int>& nums) {
4+
int score = -1, ans = 1;
5+
for (int num : nums) {
6+
score &= num;
7+
if (score == 0) {
8+
--score;
9+
++ans;
10+
}
11+
}
12+
return ans == 1 ? 1 : ans - 1;
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func maxSubarrays(nums []int) int {
2+
ans, score := 1, -1
3+
for _, num := range nums {
4+
score &= num
5+
if score == 0 {
6+
score--
7+
ans++
8+
}
9+
}
10+
if ans == 1 {
11+
return 1
12+
}
13+
return ans - 1
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def maxSubarrays(self, nums: List[int]) -> int:
3+
score, ans = -1, 1
4+
for num in nums:
5+
score &= num
6+
if score == 0:
7+
score = -1
8+
ans += 1
9+
return 1 if ans == 1 else ans - 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maxSubarrays(nums: number[]): number {
2+
let [ans, score] = [1, -1];
3+
for (const num of nums) {
4+
score &= num;
5+
if (score === 0) {
6+
--score;
7+
++ans;
8+
}
9+
}
10+
return ans == 1 ? 1 : ans - 1;
11+
}

0 commit comments

Comments
 (0)