import java.util.*;
class Main {
public int solution(int n,int k, int[] arr) {
int answer = Integer.MIN_VALUE;
for(int start=0; start<n; start++) {
int cnt = 0;
int change = k; //변경권
int end = start;
while(change >= 0 && end < n) {
if(change != 0 && arr[end] == 0) {change--; cnt++; end++;}
else if(arr[end] == 1) {cnt++; end++;}
else {break;}
}
answer = Math.max(answer, cnt);
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = kb.nextInt();
}
System.out.println(T.solution(n,k,arr));
}
}
++ 다른풀이
import java.util.*;
class Main {
public int solution(int n,int k, int[] arr) {
int answer = Integer.MIN_VALUE;
for(int start=0; start<n; start++) {
int cnt = 0;
int lt = 0;
for(int rt=0; rt<n; rt++) {
if(arr[rt] == 0) cnt++;
while(cnt>k) {
if(arr[lt] == 0) cnt--; lt++;
}
answer = Math.max(answer, rt-lt+1);
}
}
return answer;
}
public static void main(String[] args) {
Main T = new Main();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = kb.nextInt();
}
System.out.println(T.solution(n,k,arr));
}
}
rt - lt + 1이 최대 길이라는것을 내 스스로 생각해낼수 있을정도까지 열심히 공부하고 연구하자!