Thinking Process
We find all possible number of rotations to make all the values in A are the same, or all the values in B are the same, and then get the minimum among them.\n\n
> We find all possible number of rotations to make `all the values in A are the same, or all the values in B are the same`, and then get the minimum among them.\n\n>  A[i] = A[i] if not swap OR B[i] if swap, B[i] = B[i] if not swap OR A[i] if swap.\nWhen i = 0, A[0] can be either A[0] or B[0], B[0] can be either B[0] or A[0].\n>  So the `value` must be either `A[0]` or `B[0]` if can be done.\n\n> There are 4 possible cases:\n> make values in A equal to A[0]\n> make values in B equal to A[0]\n> make values in A equal to B[0]\n> make values in B equal to B[0]\n> \n> For each case we count rotations and we get the min rotations among them.\n****\n```\n    public int minDominoRotations(int[] A, int[] B) {\n        int ans = min(\n                f(A[0], A, B),\n                f(B[0], A, B),\n                f(A[0], B, A),\n                f(B[0], B, A));\n        return ans == Integer.MAX_VALUE ? -1 : ans;\n    }\n    \n    private int min(int a, int b, int c, int d) {\n        return Math.min(Math.min(Math.min(a, b), c), d);\n    }\n\n    /* Count number of rotations to make values in A equal to target. */\n    private int f(int target, int[] A, int[] B) {\n        int swap = 0;\n        for (int i = 0; i < A.length; i++) {\n            if (A[i] != target) {\n                if (B[i] != target) {\n                    return Integer.MAX_VALUE;\n                } else {\n                    swap++;\n                }\n            }\n        }\n        return swap;\n    }\n```\n
[]