-
Notifications
You must be signed in to change notification settings - Fork 0
/
볼 모으기.java
103 lines (94 loc) · 1.9 KB
/
볼 모으기.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String str = br.readLine();
int count =0;
int answer =0;
int cur = n-1;
Queue<Integer> queue = new LinkedList<>();
for(int i =n-1; i>=0; i--)
{
if(str.charAt(i)=='R' && queue.isEmpty())
{
cur = i;
}
else if(str.charAt(i)== 'B')
{
queue.offer(i);
}
else if(str.charAt(i)=='R' && !queue.isEmpty())
{
count++;
queue.offer(i);
queue.remove();
}
}
queue.clear();
int count2 =0;
for(int i =n-1; i>=0; i--)
{
if(str.charAt(i)=='B' && queue.isEmpty())
{
cur = i;
}
else if(str.charAt(i)== 'R')
{
queue.offer(i);
}
else if(str.charAt(i)=='B' && !queue.isEmpty())
{
count2++;
queue.offer(i);
queue.remove();
}
}
queue.clear();
int count3=0;
for(int i =0; i<n; i++)
{
if(str.charAt(i)=='R' && queue.isEmpty())
{
cur = i;
}
else if(str.charAt(i)== 'B')
{
queue.offer(i);
}
else if(str.charAt(i)=='R' && !queue.isEmpty())
{
count3++;
queue.offer(i);
queue.remove();
}
}
queue.clear();
int count4 =0;
for(int i =0; i<n; i++)
{
if(str.charAt(i)=='B' && queue.isEmpty())
{
cur = i;
}
else if(str.charAt(i)== 'R')
{
queue.offer(i);
}
else if(str.charAt(i)=='B' && !queue.isEmpty())
{
count4++;
queue.offer(i);
queue.remove();
}
}
int answer2 = count3 > count4? count4:count3;
answer = count > count2? count2:count;
int answer3 = answer2>answer ? answer: answer2;
System.out.println(answer3);
}
}