-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path17615-hayoung.java
68 lines (57 loc) · 1.63 KB
/
17615-hayoung.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
64
65
66
67
// 백준 / 17615 / 볼 모으기
// https://www.acmicpc.net/problem/17615
// solve:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
class Main {
private static int min = 500001;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(br.readLine());
int redBall = 0, blueBall = 0;
String ball[] = new String[n];
String[] input = br.readLine().split("");
for (int i = 0; i < n; i++) {
ball[i] = input[i];
if (ball[i].equals("R")) {
redBall++;
continue;
}
blueBall++;
}
min = Math.min(redBall, blueBall);
String color = ball[0];
int count = 1;
for (int i = 1; i < ball.length; i++) {
if (!check(color, i, count, ball, redBall, blueBall)) {
break;
}
count++;
}
color = ball[ball.length - 1];
count = 1;
for (int i = ball.length - 2; i >= 0; i--) {
if (!check(color, i, count, ball, redBall, blueBall)) {
break;
}
count++;
}
System.out.println(min);
}
private static boolean check(String color, int i, int count, String[] ball, int redBall,
int blueBall) {
if (!ball[i].equals(color)) {
if (color.equals("R")) {
min = Math.min(min, redBall - count);
} else {
min = Math.min(min, blueBall - count);
}
return false;
}
return true;
}
}