Skip to content

feat: update solutions to lc problems: No.0487,1004,2024 #3248

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

Merged
merged 1 commit into from
Jul 10, 2024
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
349 changes: 54 additions & 295 deletions solution/0400-0499/0487.Max Consecutive Ones II/README.md

Large diffs are not rendered by default.

323 changes: 54 additions & 269 deletions solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,152 +60,15 @@ The max number of consecutive ones is 4.

<!-- solution:start -->

### Solution 1
### Solution 1: Sliding Window

<!-- tabs:start -->

#### Python3

```python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = nums.count(1)
n = len(nums)
left = [0] * n
right = [0] * n
for i, v in enumerate(nums):
if v:
left[i] = 1 if i == 0 else left[i - 1] + 1
for i in range(n - 1, -1, -1):
v = nums[i]
if v:
right[i] = 1 if i == n - 1 else right[i + 1] + 1
ans = 0
for i, v in enumerate(nums):
t = 0
if i:
t += left[i - 1]
if i < n - 1:
t += right[i + 1]
ans = max(ans, t + 1)
return ans
```

#### Java

```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int n = nums.length;
int[] left = new int[n];
int[] right = new int[n];
for (int i = 0; i < n; ++i) {
if (nums[i] == 1) {
left[i] = i == 0 ? 1 : left[i - 1] + 1;
}
}
for (int i = n - 1; i >= 0; --i) {
if (nums[i] == 1) {
right[i] = i == n - 1 ? 1 : right[i + 1] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
int t = 0;
if (i > 0) {
t += left[i - 1];
}
if (i < n - 1) {
t += right[i + 1];
}
ans = Math.max(ans, t + 1);
}
return ans;
}
}
```

#### C++

```cpp
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int n = nums.size();
vector<int> left(n), right(n);
for (int i = 0; i < n; ++i) {
if (nums[i]) {
left[i] = i == 0 ? 1 : left[i - 1] + 1;
}
}
for (int i = n - 1; ~i; --i) {
if (nums[i]) {
right[i] = i == n - 1 ? 1 : right[i + 1] + 1;
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
int t = 0;
if (i) {
t += left[i - 1];
}
if (i < n - 1) {
t += right[i + 1];
}
ans = max(ans, t + 1);
}
return ans;
}
};
```

#### Go

```go
func findMaxConsecutiveOnes(nums []int) int {
n := len(nums)
left := make([]int, n)
right := make([]int, n)
for i, v := range nums {
if v == 1 {
if i == 0 {
left[i] = 1
} else {
left[i] = left[i-1] + 1
}
}
}
for i := n - 1; i >= 0; i-- {
if nums[i] == 1 {
if i == n-1 {
right[i] = 1
} else {
right[i] = right[i+1] + 1
}
}
}
ans := 0
for i := range nums {
t := 0
if i > 0 {
t += left[i-1]
}
if i < n-1 {
t += right[i+1]
}
ans = max(ans, t+1)
}
return ans
}
```
We can iterate through the array, using a variable $\textit{cnt}$ to record the current number of 0s in the window. When $\textit{cnt} > 1$, we move the left boundary of the window to the right by one position.

<!-- tabs:end -->
After the iteration ends, the length of the window is the maximum number of consecutive 1s.

<!-- solution:end -->
Note that in the process above, we do not need to loop to move the left boundary of the window to the right. Instead, we directly move the left boundary to the right by one position. This is because the problem asks for the maximum number of consecutive 1s, so the length of the window will only increase, not decrease. Therefore, we do not need to loop to move the left boundary to the right.

<!-- solution:start -->

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

<!-- tabs:start -->

Expand All @@ -214,38 +77,28 @@ func findMaxConsecutiveOnes(nums []int) int {
```python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = 1
cnt = j = 0
for i, v in enumerate(nums):
if v == 0:
cnt += 1
while cnt > 1:
if nums[j] == 0:
cnt -= 1
j += 1
ans = max(ans, i - j + 1)
return ans
l = cnt = 0
for x in nums:
cnt += x ^ 1
if cnt > 1:
cnt -= nums[l] ^ 1
l += 1
return len(nums) - l
```

#### Java

```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int j = 0, cnt = 0;
int ans = 1;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) {
++cnt;
int l = 0, cnt = 0;
for (int x : nums) {
cnt += x ^ 1;
if (cnt > 1) {
cnt -= nums[l++] ^ 1;
}
while (cnt > 1) {
if (nums[j++] == 0) {
--cnt;
}
}
ans = Math.max(ans, i - j + 1);
}
return ans;
return nums.length - l;
}
}
```
Expand All @@ -256,20 +109,14 @@ class Solution {
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int ans = 1;
int cnt = 0, j = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) {
++cnt;
}
while (cnt > 1) {
if (nums[j++] == 0) {
--cnt;
}
int l = 0, cnt = 0;
for (int x : nums) {
cnt += x ^ 1;
if (cnt > 1) {
cnt -= nums[l++] ^ 1;
}
ans = max(ans, i - j + 1);
}
return ans;
return nums.size() - l;
}
};
```
Expand All @@ -278,114 +125,52 @@ public:

```go
func findMaxConsecutiveOnes(nums []int) int {
ans := 1
j, cnt := 0, 0
for i, v := range nums {
if v == 0 {
cnt++
}
for cnt > 1 {
if nums[j] == 0 {
cnt--
}
j++
l, cnt := 0, 0
for _, x := range nums {
cnt += x ^ 1
if cnt > 1 {
cnt -= nums[l] ^ 1
l++
}
ans = max(ans, i-j+1)
}
return ans
return len(nums) - l
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 3

<!-- tabs:start -->

#### Python3

```python
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
l = r = 0
k = 1
while r < len(nums):
if nums[r] == 0:
k -= 1
if k < 0:
if nums[l] == 0:
k += 1
l += 1
r += 1
return r - l
```
#### TypeScript

#### Java

```java
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int l = 0, r = 0;
int k = 1;
while (r < nums.length) {
if (nums[r++] == 0) {
--k;
}
if (k < 0 && nums[l++] == 0) {
++k;
}
```ts
function findMaxConsecutiveOnes(nums: number[]): number {
let [l, cnt] = [0, 0];
for (const x of nums) {
cnt += x ^ 1;
if (cnt > 1) {
cnt -= nums[l++] ^ 1;
}
return r - l;
}
return nums.length - l;
}
```

#### C++

```cpp
class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int l = 0, r = 0;
int k = 1;
while (r < nums.size()) {
if (nums[r++] == 0) {
--k;
}
if (k < 0 && nums[l++] == 0) {
++k;
}
#### JavaScript

```js
/**
* @param {number[]} nums
* @return {number}
*/
var findMaxConsecutiveOnes = function (nums) {
let [l, cnt] = [0, 0];
for (const x of nums) {
cnt += x ^ 1;
if (cnt > 1) {
cnt -= nums[l++] ^ 1;
}
return r - l;
}
return nums.length - l;
};
```

#### Go

```go
func findMaxConsecutiveOnes(nums []int) int {
l, r := 0, 0
k := 1
for ; r < len(nums); r++ {
if nums[r] == 0 {
k--
}
if k < 0 {
if nums[l] == 0 {
k++
}
l++
}
}
return r - l
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading