-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximumSequenceWithOneZero.java
More file actions
37 lines (27 loc) · 1.08 KB
/
Copy pathMaximumSequenceWithOneZero.java
File metadata and controls
37 lines (27 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package interview;
import java.util.List;
import java.util.Objects;
public class MaximumSequenceWithOneZero {
/**
* Дан массив из 0 и 1. Надо вывести максимальную последовательность 1 в этом массиве, при условии,
* что она может быть разделена только одним 0. Пример вывода: 1,0,1,1,1,0,0,1,1,1 -> 4.
*/
public static int maximumSequenceWithOneZero(List<Integer> b) {
if (Objects.isNull(b) || b.isEmpty()) return 0;
if (b.size() == 1 && b.get(0) == 1) return 1;
if (b.size() == 1 && b.get(0) == 0) return 0;
int maxCount = 0;
int previousCount = 0;
int currentCount = 0;
for (Integer value : b) {
if (value == 1) {
currentCount++;
maxCount = Math.max(maxCount, previousCount + currentCount);
} else {
previousCount = currentCount;
currentCount = 0;
}
}
return maxCount;
}
}