- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitFlip.java
More file actions
Latest commit
44 lines (39 loc) · 1.13 KB
/
Copy pathBitFlip.java
File metadata and controls
44 lines (39 loc) · 1.13 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
38
39
40
41
42
43
44
publicclassBitFlip {
publicstaticintbitFlip(int[] array, intlength) {
intleft = 0, right = length - 1;
intres = 0;
while (left <= right) {
while (array[left] == 1) {
left++;
}
while (array[right] == 1) {
right--;
}
intcf = countFlip(array, left, right);
res = res > cf ? res : cf;
while (array[left] == 0) {
left++;
}
while (array[right] == 0) {
right--;
}
}
returnres;
}
privatestaticintcountFlip(int[] array, intleft, intright) {
intcount = 0;
for (inti = 0; i < left; i++) {
if (array[i] == 1) count++;
}
for (inti = left; i <= right; i++) {
if (array[i] == 0) count++;
}
for (inti = right + 1; i < array.length; i++) {
if (array[i] == 1) count++;
}
returncount;
}
publicstaticvoidmain(String[] args) {
System.out.println(bitFlip(newint[] {1, 0, 0, 1, 0, 0, 1, 0}, 8));
}
}