Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created February 6, 2020 22:18
Show Gist options
  • Save RP-3/eeec866aa65a87faabbb060032e2ad9b to your computer and use it in GitHub Desktop.
Save RP-3/eeec866aa65a87faabbb060032e2ad9b to your computer and use it in GitHub Desktop.
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