// 최대 길이 연속부분수열
public class P0306 {
private static int solution(int n, int k, int[] arr) {
int l = 0;
int answer = 0;
int count = 0;
for (int r = 0; r < arr.length; r++) {
if (arr[r] == 0) {
count++;
}
while (count > k) {
if (arr[l] == 0) {
count--;
}
l++;
}
answer = Math.max(answer, r - l + 1);
}
return answer;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split("\\s");
int n = Integer.parseInt(split[0]);
int k = Integer.parseInt(split[1]);
String[] split2 = br.readLine().split("\\s");
int[] arr = new int[split2.length];
for (int i = 0; i < split2.length; i++) {
arr[i] = Integer.parseInt(split2[i]);
}
int answer = solution(n, k, arr);
System.out.println(answer);
}
}
📌 문제
⭐️ 아이디어
l
과r
을 두고 최대 길이를 구한다🤔 고민한 내용
count > k
가 되었을 때l
을 어떻게 처리해줘야하지..?💪 새롭게 배운 내용
🆘 이해가 어려운 내용
❌ 해결하지 못한 이유
✅ 본인 풀이
참고한 자료