-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
37 lines (35 loc) · 1.01 KB
/
solution.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
function solution(A) {
var n = A.length;
var result = 0;
if(n == 1) return 0; // handle array only one element
// count number of the same face Coin
for (let i = 0; i < n - 1; i++) {
if(A[i] == A[i + 1]) {
result = result + 1;
}
}
if(result == n - 1) {
return result - 1; // handle array has two elements which same value [0, 0], [1, 1]
}
// for flip one Coin
var r = 0;
for(let i = 0; i < n; i++) {
var count = 0;
// check curr Coin with prev Coin
if(i > 0) {
if(A[i] != A[i - 1])
count = count + 1;
else
count = count - 1; // already count because same face
}
// check curr Coin with next Coin
if(i < n - 1) {
if(A[i] != A[i + 1])
count = count + 1;
else
count = count - 1; // already count because same face
}
r = Math.max(r, coin);
}
return result + r;
}