File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed
Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+
4+ public class Main {
5+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+ private static char[] input;
8+ private static int[] a;
9+ private static int total, answer, rCount;
10+
11+ public static void main(String[] args) throws IOException {
12+ init();
13+ answer = twoPointer();
14+
15+ bw.write(answer + "\n");
16+ bw.flush();
17+ bw.close();
18+ br.close();
19+ }
20+
21+ private static void init() throws IOException {
22+ input = br.readLine().toCharArray();
23+
24+ rCount = 0;
25+ for (char c : input) {
26+ if (c == 'R') rCount++;
27+ }
28+
29+ a = new int[rCount];
30+
31+ int idx = 0;
32+ int kCount = 0;
33+
34+ for (int i = 0; i < input.length; i++) {
35+ if (input[i] == 'K') {
36+ total++;
37+ kCount++;
38+ } else {
39+ a[idx++] = kCount;
40+ }
41+ }
42+ }
43+
44+ private static int twoPointer() {
45+ if (rCount == 0) return 0;
46+
47+ int left = 0;
48+ int right = rCount - 1;
49+ int max = 0;
50+
51+ while (left <= right) {
52+ int leftK = a[left];
53+ int rightK = total - a[right];
54+
55+ int k = Math.min(leftK, rightK);
56+ int r = right - left + 1;
57+
58+ max = Math.max(max, k * 2 + r);
59+
60+ if (leftK < rightK) {
61+ left++;
62+ } else {
63+ right--;
64+ }
65+ }
66+
67+ return max;
68+ }
69+ }
70+ ```
You can’t perform that action at this time.
0 commit comments