-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMax1sAfterModification.java
63 lines (47 loc) · 1.56 KB
/
Max1sAfterModification.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
You are given with an array of 1s and 0s. And you are given with an integer M, which signifies number of flips allowed.
Find the position of zeros which when flipped will produce maximum continuous series of 1s.
For this problem, return the indices of maximum continuous series of 1s in order.
Example:
Input :
Array = {1 1 0 1 1 0 0 1 1 1 }
M = 1
Output :
[0, 1, 2, 3, 4]
If there are multiple possible solutions, return the sequence which has the minimum start index.
*/
public class Solution {
public ArrayList<Integer> maxone(ArrayList<Integer> A, int B) {
int i=0,j=0,c=B;
int start=0,end=0;
while(i<A.size() && j<A.size()){
if(A.get(j)==1){
j++;
}else if(A.get(j)==0 && c>0){
j++;
c--;
}else if(A.get(j)==0 && c<=0){
int currLength = j-i;
int maxLength = end-start;
if(currLength>maxLength){
start=i;
end=j;
}
i++;
j=i;
c=B;
}
}
int currLength = j-i;
int maxLength = end-start;
if(currLength>maxLength){
start=i;
end=j;
}
ArrayList<Integer> result = new ArrayList<>();
for(int k=start;k<end;k++){
result.add(k);
}
return result;
}
}