Minimum Domino Rotations For Equal Row | Java
\nclass Solution \n{\n    /* In order to rotate the dominoes as defined by the problem every domino must have a common value\n     * which can reside on either 
```\nclass Solution \n{\n    /* In order to rotate the dominoes as defined by the problem every domino must have a common value\n     * which can reside on either it\'s upper or lower face.  If there is a domino whithout a common \n     * value the problem can not be solved and a -1 should be returned.  Given that, if you take the \n     * first domino in the list either it\'s upper and/or it\'s lower value must exist on every other\n     * domino or the problem can\'t be solved. Iterate down the length of both the A and B arrays \n     * simultaniously looking for this error condition, returning -1 if found, and call it with the \n     * upper value of the first domino (i.e. A[0]) and then with the lower value (i.e. B[0].)\n     *\n     * At the same time keep a running count of values in A and B which are not equal to the value you\n     * are checking against that do not equal the value you\'re checking for.  If at the end of the \n     * pass over the arrays an error condition wasn\'t found then a solution is possible and the count\n     * indicatates the number of rotations which must occur, the A count indicates move lower values \n     * up and the B value for moving upper values down.  Return the lower of these values.\n     *\n     * Once this check/count has been run for both the upper and lower value of the first domino \n     * return the greater of the two values.  At first it may seem counter-intuitive to use Math.max \n     * for the return value, but it is correct.  At this point you\'ll be in one of three states; Niether \n     * value provides a solution and both results are -1 in which case -1 gets returned, either the \n     * upper or lower value is the solution and the other fails in this case you need to return the \n     * greater value as the other is -1, or both the upper and lower are correct soutions which will \n     * only happen if all have the same values so the counts will be equal.\n     */\n    \n    public int minDominoRotations(int[] A, int[] B) \n    {\n        return Math.max( count( A[ 0 ], A, B ), count( B[ 0 ], A, B ) );\n    }\n    \n    private static int count( int pips, int[] A, int[] B )\n    {\n        int     aCnt    = 0;\n        int     bCnt    = 0;\n        \n        for( int i = 0; i < A.length; i++ )\n        {\n            if( A[ i ] != pips && B[ i ] != pips )\n            {\n                return -1;\n            }\n            \n            if( A[ i ] != pips )    aCnt++;\n            if( B[ i ] != pips )    bCnt++;\n        }\n        \n        return Math.min( aCnt, bCnt );\n    }\n}\n```
[]