https://www.acmicpc.net/problem/22857
1. 유형
- 투포인터
2. 풀이
- 투포인터 변수 R, L을 잡는다. 시작 인덱스는 0번
- R, L의 사이에 홀수의 갯수가 K개 초과면 L증가, 반대는 R증가
3. 코드
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N =stoi(st.nextToken());
int K = stoi(st.nextToken());
int arr[] = new int[N];
st = new StringTokenizer(br.readLine());
for(int i=0; i<N; i++) {
arr[i] = stoi(st.nextToken());
}
int R=0,L=0;
int odd=0;
int even=0;
if(arr[0]%2==0) even++;
else odd++;
int answer = even;
while(R>=L) {
if(odd>K) {
if(arr[L]%2==0) even--;
else odd--;
L++;
}else {
R++;
if(R>=N) break;
if(arr[R]%2==0) even++;
else odd++;
answer = Math.max(answer, even);
}
}
System.out.println(answer);
}
static int stoi(String s) {
return Integer.valueOf(s);
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 2609 - 최대공약수와 최소공배수 (0) | 2022.04.06 |
---|---|
[백준 18311] 왕복 (0) | 2022.03.10 |
[백준] 1446 지름길 (0) | 2022.03.06 |
[백준 1251] 단어 나누기 (0) | 2022.03.03 |
[백준 9996] 한국이 그리울 땐 서버에 접속하지 (0) | 2022.03.03 |