Created
February 6, 2020 22:18
-
-
Save RP-3/eeec866aa65a87faabbb060032e2ad9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var minDominoRotations = function(A, B) { | |
const minRotationsToSetAllTo = (x) => { | |
let aSame = 0; | |
let bSame = 0; | |
for(let i=0; i<A.length; i++){ | |
if(A[i] !== x){ | |
if(B[i] === x) aSame++; | |
else aSame = Infinity; | |
} | |
if(B[i] !== x){ | |
if(A[i] === x) bSame++; | |
else bSame = Infinity; | |
} | |
if(aSame === Infinity && bSame === Infinity) break; | |
} | |
return Math.min(aSame, bSame); | |
}; | |
const minRotations = Math.min( | |
minRotationsToSetAllTo(A[0]), | |
minRotationsToSetAllTo(B[0]) | |
); | |
return minRotations === Infinity ? -1 : minRotations; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment