Skip to content

Commit ddc7121

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0487
1 parent c5d0367 commit ddc7121

File tree

5 files changed

+226
-4
lines changed

5 files changed

+226
-4
lines changed

README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<a href="https://opencollective.com/doocs-leetcode/backers/badge.svg" alt="backers on Open Collective"><img src="https://opencollective.com/doocs-leetcode/backers/badge.svg" /></a>
88
<a href="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" alt="Sponsors on Open Collective"><img src="https://opencollective.com/doocs-leetcode/sponsors/badge.svg" /></a>
99
<a href="https://github.com/doocs/leetcode/blob/main/LICENSE"><img src="https://badgen.net/github/license/doocs/leetcode?color=green" alt="LICENSE"></a><br>
10-
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode?color=cyan" alt="stars"></a>
11-
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode?color=cyan" alt="forks"></a>
10+
<a href="https://github.com/doocs/leetcode/stargazers"><img src="https://badgen.net/github/stars/doocs/leetcode?color=cyan&cache=300" alt="stars"></a>
11+
<a href="https://github.com/doocs/leetcode/network/members"><img src="https://badgen.net/github/forks/doocs/leetcode?color=cyan&cache=300" alt="forks"></a>
1212
<a href="https://github.com/doocs/leetcode"><img src="https://badgen.net/badge/⭐/GitHub/cyan" alt="github"></a>
1313
<a href="https://gitee.com/doocs/leetcode"><img src="https://badgen.net/badge/⭐/Gitee/cyan" alt="github"></a>
1414
<a href="http://makeapullrequest.com"><img src="https://badgen.net/badge/PRs/welcome/cyan" alt="PRs Welcome"></a>

solution/0400-0499/0487.Max Consecutive Ones II/README.md

+86-1
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,107 @@
3333

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

36+
`prefix[i]` 数组表示以 i 结尾往前累计的最大连续 1 的个数,`suffix[i]` 数组表示以 i 开头往后累计的最大连续 1 的个数。
37+
38+
遍历 `nums` 数组每个为 0 的位置,则位置 i 的最大连续 1 的个数为 `1 + prefix[i-1] + suffix[i+1]`
39+
40+
当然,如果 `nums` 数组没有 0,即所有元素都是 1,那么结果即为 `nums` 数组的长度。
41+
3642
<!-- tabs:start -->
3743

3844
### **Python3**
3945

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

4248
```python
43-
49+
class Solution:
50+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
51+
n = len(nums)
52+
prefix = [0] * n
53+
suffix = [0] * n
54+
res = 0
55+
for i in range(n):
56+
if i == 0:
57+
prefix[i] = nums[i]
58+
else:
59+
prefix[i] = 0 if nums[i] == 0 else prefix[i - 1] + 1
60+
res = max(res, prefix[i])
61+
62+
for i in range(n - 1, -1, -1):
63+
if i == n - 1:
64+
suffix[i] = nums[i]
65+
else:
66+
suffix[i] = 0 if nums[i] == 0 else suffix[i + 1] + 1
67+
68+
for i in range(n):
69+
if nums[i] == 0:
70+
t = 1
71+
if i > 0:
72+
t += prefix[i - 1]
73+
if i < n - 1:
74+
t += suffix[i + 1]
75+
res = max(res, t)
76+
return res
4477
```
4578

4679
### **Java**
4780

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

83+
- 双指针,时间复杂度 O(n²),空间复杂度 O(1)
84+
5085
```java
86+
class Solution {
87+
public int findMaxConsecutiveOnes(int[] nums) {
88+
int n = nums.length;
89+
int res = 0;
90+
for (int i = 0; i < n; ++i) {
91+
int cnt = 1;
92+
int j = i;
93+
while (j < n && (cnt > 0 || nums[j] == 1)) {
94+
if (nums[j] == 0) --cnt;
95+
++j;
96+
}
97+
res = Math.max(res, j - i);
98+
}
99+
return res;
100+
}
101+
}
102+
```
51103

104+
- 辅助数组,时间复杂度 O(n),空间复杂度 O(n)
105+
106+
```java
107+
class Solution {
108+
public int findMaxConsecutiveOnes(int[] nums) {
109+
int n = nums.length;
110+
111+
int[] prefix = new int[n];
112+
int[] suffix = new int[n];
113+
114+
int res = 0;
115+
for (int i = 0; i < n; ++i) {
116+
if (i == 0) prefix[0] = nums[0];
117+
else prefix[i] = nums[i] == 0 ? 0 : prefix[i - 1] + 1;
118+
res = Math.max(res, prefix[i]);
119+
}
120+
121+
for (int i = n - 1; i >= 0; --i) {
122+
if (i == n - 1) suffix[n - 1] = nums[n - 1];
123+
else suffix[i] = nums[i] == 0 ? 0 : suffix[i + 1] + 1;
124+
}
125+
126+
for (int i = 0; i < n; ++i) {
127+
if (nums[i] == 0) {
128+
int t = 1;
129+
if (i > 0) t += prefix[i - 1];
130+
if (i < n - 1) t += suffix[i + 1];
131+
res = Math.max(res, t);
132+
}
133+
}
134+
return res;
135+
}
136+
}
52137
```
53138

54139
### **...**

solution/0400-0499/0487.Max Consecutive Ones II/README_EN.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,92 @@ What if the input numbers come in one by one as an <b>infinite stream</b>? In ot
3535
### **Python3**
3636

3737
```python
38-
38+
class Solution:
39+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
40+
n = len(nums)
41+
prefix = [0] * n
42+
suffix = [0] * n
43+
res = 0
44+
for i in range(n):
45+
if i == 0:
46+
prefix[i] = nums[i]
47+
else:
48+
prefix[i] = 0 if nums[i] == 0 else prefix[i - 1] + 1
49+
res = max(res, prefix[i])
50+
51+
for i in range(n - 1, -1, -1):
52+
if i == n - 1:
53+
suffix[i] = nums[i]
54+
else:
55+
suffix[i] = 0 if nums[i] == 0 else suffix[i + 1] + 1
56+
57+
for i in range(n):
58+
if nums[i] == 0:
59+
t = 1
60+
if i > 0:
61+
t += prefix[i - 1]
62+
if i < n - 1:
63+
t += suffix[i + 1]
64+
res = max(res, t)
65+
return res
3966
```
4067

4168
### **Java**
4269

70+
- Two Pointers, time complexity: O(n²), memory complexity: O(1)
71+
4372
```java
73+
class Solution {
74+
public int findMaxConsecutiveOnes(int[] nums) {
75+
int n = nums.length;
76+
int res = 0;
77+
for (int i = 0; i < n; ++i) {
78+
int cnt = 1;
79+
int j = i;
80+
while (j < n && (cnt > 0 || nums[j] == 1)) {
81+
if (nums[j] == 0) --cnt;
82+
++j;
83+
}
84+
res = Math.max(res, j - i);
85+
}
86+
return res;
87+
}
88+
}
89+
```
4490

91+
- Prefix Array & Suffix Array, time complexity: O(n), memory complexity: O(n)
92+
93+
```java
94+
class Solution {
95+
public int findMaxConsecutiveOnes(int[] nums) {
96+
int n = nums.length;
97+
98+
int[] prefix = new int[n];
99+
int[] suffix = new int[n];
100+
101+
int res = 0;
102+
for (int i = 0; i < n; ++i) {
103+
if (i == 0) prefix[0] = nums[0];
104+
else prefix[i] = nums[i] == 0 ? 0 : prefix[i - 1] + 1;
105+
res = Math.max(res, prefix[i]);
106+
}
107+
108+
for (int i = n - 1; i >= 0; --i) {
109+
if (i == n - 1) suffix[n - 1] = nums[n - 1];
110+
else suffix[i] = nums[i] == 0 ? 0 : suffix[i + 1] + 1;
111+
}
112+
113+
for (int i = 0; i < n; ++i) {
114+
if (nums[i] == 0) {
115+
int t = 1;
116+
if (i > 0) t += prefix[i - 1];
117+
if (i < n - 1) t += suffix[i + 1];
118+
res = Math.max(res, t);
119+
}
120+
}
121+
return res;
122+
}
123+
}
45124
```
46125

47126
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public int findMaxConsecutiveOnes(int[] nums) {
3+
int n = nums.length;
4+
5+
int[] prefix = new int[n];
6+
int[] suffix = new int[n];
7+
8+
int res = 0;
9+
for (int i = 0; i < n; ++i) {
10+
if (i == 0) prefix[0] = nums[0];
11+
else prefix[i] = nums[i] == 0 ? 0 : prefix[i - 1] + 1;
12+
res = Math.max(res, prefix[i]);
13+
}
14+
15+
for (int i = n - 1; i >= 0; --i) {
16+
if (i == n - 1) suffix[n - 1] = nums[n - 1];
17+
else suffix[i] = nums[i] == 0 ? 0 : suffix[i + 1] + 1;
18+
}
19+
20+
for (int i = 0; i < n; ++i) {
21+
if (nums[i] == 0) {
22+
int t = 1;
23+
if (i > 0) t += prefix[i - 1];
24+
if (i < n - 1) t += suffix[i + 1];
25+
res = Math.max(res, t);
26+
}
27+
}
28+
return res;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
3+
n = len(nums)
4+
prefix = [0] * n
5+
suffix = [0] * n
6+
res = 0
7+
for i in range(n):
8+
if i == 0:
9+
prefix[i] = nums[i]
10+
else:
11+
prefix[i] = 0 if nums[i] == 0 else prefix[i - 1] + 1
12+
res = max(res, prefix[i])
13+
14+
for i in range(n - 1, -1, -1):
15+
if i == n - 1:
16+
suffix[i] = nums[i]
17+
else:
18+
suffix[i] = 0 if nums[i] == 0 else suffix[i + 1] + 1
19+
20+
for i in range(n):
21+
if nums[i] == 0:
22+
t = 1
23+
if i > 0:
24+
t += prefix[i - 1]
25+
if i < n - 1:
26+
t += suffix[i + 1]
27+
res = max(res, t)
28+
return res

0 commit comments

Comments
 (0)