-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (84 loc) · 2.47 KB
/
index.js
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
// score:
function adjustLights(lights) {
let firstLight = lights[0];
let secondLight = firstLight !== '🟢' ? '🟢' : '🔴';
let changeLight = 0;
for (let i = 0; i < lights.length; i+=2) {
if (firstLight !== lights[i]) changeLight++;
}
for (let j = 1; j < lights.length; j+=2) {
if (secondLight !== lights[j]) changeLight++;
}
return changeLight;
}
// score: 370
// function adjustLights(lights) {
// let a = 0;
// let b = 0;
// let x = ["🔴", "🟢"];
// lights.forEach((l, i) => (l == x[i & 1] ? a++ : b++));
// return Math.min(a, b);
// }
// score: 390 Achalogy
// function adjustLights(lights) {
// let start = "";
// let res = 0;
// for (let l of lights) {
// res += +(l == start);
// start = [l, " "][+(l == start)];
// }
// return res;
// }
// score: 280 hecho con bitwise operators
// function adjustLights(lights) {
// let a = 0, b = 0;
// const length = lights.length;
// for (let i = 0, n = 0; i < length; i++) {
// n = (i % 2) - +(lights[i] === "🟢");
// a += Math.abs(n);
// }
// b = length - a;
// return Math.min(a, b);
// }
// score: 370
// function adjustLights(lights) {
// const leds = {
// "🔴": "🟢",
// "🟢": "🔴",
// };
// return lights.reduceRight(
// (acc, light, index) => {
// if (light === lights[index - 1]) {
// acc++;
// lights[index - 1] = leds[light];
// }
// return acc;
// },
// 0
// );
// }
// score: 390
// function adjustLights(lights) {
// const init = lights[0];
// let rev = false;
// let count1 = 0;
// let count2 = 0;
// for (const light of lights) {
// count1 += rev == (light == init);
// rev = !rev;
// }
// rev = true;
// for (const light of lights) {
// count2 += rev == (light == init);
// rev = !rev;
// }
// count1 = Math.min(count1, count2);
// return count1;
// }
module.exports = adjustLights;
console.log(adjustLights(['🟢', '🔴', '🟢', '🟢', '🟢'])); // 1
console.log(adjustLights(["🔴", "🔴", "🟢", "🟢", "🔴"])); // 2
console.log(adjustLights(['🟢', '🔴', '🟢', '🔴', '🟢'])); // 0
console.log(adjustLights(['🟢', '🔴', '🔴', '🟢', '🔴'])); // 2
console.log(adjustLights(['🔴', '🔴', '🟢', '🔴', '🟢'])); // 1
// -> 1 (cambias la cuarta luz a 🔴)