반응형
문제
https://www.acmicpc.net/problem/17615
JAVA 코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
private static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
char[] balls = br.readLine().toCharArray();
int red = 0;
int blue = 0;
for (int idx = 0; idx < N; idx++) {
if (balls[idx] == 'R')
red++;
else
blue++;
}
int result = Integer.MAX_VALUE;
// left 'R'
int idx = 0;
int cnt = 0;
while (isValid(idx) && balls[idx++] == 'R') {
cnt++;
}
result = Math.min(result, red - cnt);
// right 'R'
idx = N - 1;
cnt = 0;
while (isValid(idx) && balls[idx--] == 'R') {
cnt++;
}
result = Math.min(result, red - cnt);
// left 'B'
idx = 0;
cnt = 0;
while (isValid(idx) && balls[idx++] == 'B') {
cnt++;
}
result = Math.min(result, blue - cnt);
// right 'B'
idx = N - 1;
cnt = 0;
while (isValid(idx) && balls[idx--] == 'B') {
cnt++;
}
result = Math.min(result, blue - cnt);
bw.write(String.valueOf(result));
br.close();
bw.flush();
bw.close();
}
public static boolean isValid(int idx) {
if (idx >= N || idx < 0) {
return false;
}
return true;
}
}
반응형
'Algorithm > 문제 풀이' 카테고리의 다른 글
[BAEKJOON_15723 - JAVA] n단 논법 (0) | 2023.06.16 |
---|---|
[BAEKJOON_11726 - JAVA] 2×n 타일링 (0) | 2022.12.07 |
[BAEKJOON_12865 - JAVA] 평범한 배낭 (0) | 2022.11.24 |
[BAEKJOON_26042 - JAVA] 식당 입구 대기 줄 (0) | 2022.11.24 |
[BAEKJOON_26005 - JAVA] 나뭇잎 학회 (0) | 2022.11.23 |