-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBrogramming Contest.js
More file actions
43 lines (37 loc) · 870 Bytes
/
Brogramming Contest.js
File metadata and controls
43 lines (37 loc) · 870 Bytes
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
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let t,
testCases = [],
currentCase = 0;
rl.on("line", (line) => {
if (typeof t === "undefined") {
t = parseInt(line.trim());
} else {
testCases.push(line.trim());
if (testCases.length === t * 2) {
rl.close();
}
}
});
rl.on("close", () => {
for (let i = 0; i < testCases.length; i += 2) {
const n = parseInt(testCases[i]);
const s = testCases[i + 1];
console.log(solution(n, s));
}
});
function solution(n, s = "") {
let minimumMove = 0;
let searchingFor = "1";
for (let i = 0; i < s.length; i++) {
if (s[i] === searchingFor) {
minimumMove += 1;
searchingFor = searchingFor === "1" ? "0" : "1";
}
}
return minimumMove;
}
// console.log(solution(3, "001"));