Skip to content

Commit b2fd509

Browse files
committed
perf(p2190A): optimize string sorting logic and character counting
1 parent 2104dd0 commit b2fd509

File tree

1 file changed

+12
-13
lines changed
  • src/main/java/com/lzw/solutions/codeforces/p2190A

1 file changed

+12
-13
lines changed

src/main/java/com/lzw/solutions/codeforces/p2190A/Main.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,28 @@ void solve() throws IOException {
2626
int n = Integer.parseInt(in.readLine());
2727
String s = in.readLine();
2828
assert (s.length() == n);
29-
boolean allZeros = true;
3029
int i;
31-
for (i = 0; i < n; i++) {
32-
if (s.charAt(i) == '1') {
33-
allZeros = false;
34-
}
35-
}
36-
boolean allOnes = true;
30+
int zero = 0, one = 0;
3731
for (i = 0; i < n; i++) {
3832
if (s.charAt(i) == '0') {
39-
allOnes = false;
33+
zero++;
34+
} else {
35+
one++;
4036
}
4137
}
42-
if (allZeros || allOnes) {
38+
if (zero == n || one == n) {
4339
out.println("Bob");
4440
} else {
4541
out.println("Alice");
46-
char[] chars = s.toCharArray();
47-
char[] sortedChars = chars.clone();
48-
Arrays.sort(sortedChars);
4942
ArrayList<Integer> positions = new ArrayList<>();
5043
for (i = 0; i < n; i++) {
51-
if (chars[i] != sortedChars[i]) {
44+
char target;
45+
if (i < zero) {
46+
target = '0';
47+
} else {
48+
target = '1';
49+
}
50+
if (s.charAt(i) != target) {
5251
positions.add(i);
5352
}
5453
}

0 commit comments

Comments
 (0)