Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.2871 #1734

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,28 @@

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

**方法一:贪心 + 位运算**

我们初始化一个变量 $score$,用来记录当前子数组的分数,初始时 $score = -1$。然后我们遍历数组,对于每个元素 $num$,我们将 $score$ 与 $num$ 进行按位与运算,然后将结果赋值给 $score$。如果 $score = 0$,说明当前子数组的分数为 $0$,我们就可以将当前子数组分割出来,然后将 $score$ 重置为 $-1$。最后我们返回分割出的子数组的个数。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
score, ans = -1, 1
for num in nums:
score &= num
if score == 0:
score = -1
ans += 1
return 1 if ans == 1 else ans - 1
```

### **Java**
Expand All @@ -92,19 +106,55 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int maxSubarrays(vector<int>& nums) {
int score = -1, ans = 1;
for (int num : nums) {
score &= num;
if (score == 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}
};
```

### **Go**

```go

func maxSubarrays(nums []int) int {
ans, score := 1, -1
for _, num := range nums {
score &= num
if score == 0 {
score--
ans++
}
}
if ans == 1 {
return 1
}
return ans - 1
}
```

### **TypeScript**

```ts

function maxSubarrays(nums: number[]): number {
let [ans, score] = [1, -1];
for (const num of nums) {
score &= num;
if (score === 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,26 @@ It can be shown that we cannot split the array into more than 1 subarray with a

## Solutions

**Solution 1: Greedy + Bitwise Operation**

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.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
score, ans = -1, 1
for num in nums:
score &= num
if score == 0:
score = -1
ans += 1
return 1 if ans == 1 else ans - 1
```

### **Java**
Expand All @@ -82,19 +96,55 @@ class Solution {
### **C++**

```cpp

class Solution {
public:
int maxSubarrays(vector<int>& nums) {
int score = -1, ans = 1;
for (int num : nums) {
score &= num;
if (score == 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}
};
```

### **Go**

```go

func maxSubarrays(nums []int) int {
ans, score := 1, -1
for _, num := range nums {
score &= num
if score == 0 {
score--
ans++
}
}
if ans == 1 {
return 1
}
return ans - 1
}
```

### **TypeScript**

```ts

function maxSubarrays(nums: number[]): number {
let [ans, score] = [1, -1];
for (const num of nums) {
score &= num;
if (score === 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int maxSubarrays(vector<int>& nums) {
int score = -1, ans = 1;
for (int num : nums) {
score &= num;
if (score == 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
func maxSubarrays(nums []int) int {
ans, score := 1, -1
for _, num := range nums {
score &= num
if score == 0 {
score--
ans++
}
}
if ans == 1 {
return 1
}
return ans - 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def maxSubarrays(self, nums: List[int]) -> int:
score, ans = -1, 1
for num in nums:
score &= num
if score == 0:
score = -1
ans += 1
return 1 if ans == 1 else ans - 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function maxSubarrays(nums: number[]): number {
let [ans, score] = [1, -1];
for (const num of nums) {
score &= num;
if (score === 0) {
--score;
++ans;
}
}
return ans == 1 ? 1 : ans - 1;
}