[Java/C++/Python] Different Ideas
Intuition\nOne observation is that, if A[0] works, no need to check B[0].\nBecause if both A[0] and B[0] exist in all dominoes,\nwhen you swap A[0] in a whole r
## Intuition\nOne observation is that, if `A[0]` works, no need to check `B[0]`.\nBecause if both `A[0]` and `B[0]` exist in all dominoes,\nwhen you swap `A[0]` in a whole row,\nyou will swap  `B[0]` in a whole at the same time.\nThe result of trying  `A[0]` and `B[0]` will be the same.\n<br>\n\n## Solution 1:\n\nCount the occurrence of all numbers in `A` and `B`,\nand also the number of domino with two same numbers.\n\nTry all possibilities from 1 to 6.\nIf we can make number `i` in a whole row,\nit should satisfy that `countA[i] + countB[i] - same[i] = n`\n\nTake example of\n`A = [2,1,2,4,2,2]`\n`B = [5,2,6,2,3,2]`\n\n`countA[2] = 4`, as `A[0] = A[2] = A[4] = A[5] = 2`\n`countB[2] = 3`, as `B[1] = B[3] = B[5] = 2`\n`same[2] = 1`, as `A[5] = B[5] = 2`\n\nWe have `countA[2] + countB[2] - same[2] =  6`,\nso we can make 2 in a whole row.\n\nTime `O(N)`, Space `O(1)`\n<br>\n\n**Java**\n```java\n    public int minDominoRotations(int[] A, int[] B) {\n        int[] countA = new int[7], countB = new int[7], same = new int[7];\n        int n = A.length;\n        for (int i = 0; i < n; ++i) {\n            countA[A[i]]++;\n            countB[B[i]]++;\n            if (A[i] == B[i])\n                same[A[i]]++;\n        }\n        for (int i  = 1; i < 7; ++i)\n            if (countA[i] + countB[i] - same[i] == n)\n                return n - Math.max(countA[i], countB[i]);\n        return -1;\n    }\n```\n**C++**\n```cpp\n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        vector<int> countA(7), countB(7), same(7);\n        int n = A.size();\n        for (int i = 0; i < n; ++i) {\n            countA[A[i]]++;\n            countB[B[i]]++;\n            if (A[i] == B[i])\n                same[A[i]]++;\n        }\n        for (int i  = 1; i < 7; ++i)\n            if (countA[i] + countB[i] - same[i] == n)\n                return n - max(countA[i], countB[i]);\n        return -1;\n    }\n```\n**python**\n```python\n    def minDominoRotations(self, A, B):\n        for x in [A[0],B[0]]:\n            if all(x in d for d in zip(A, B)):\n                return len(A) - max(A.count(x), B.count(x))\n        return -1\n```\n<br>\n\n## Solution 2\n1. Try make `A[0]` in a whole row,\nthe condition is that `A[i] == A[0] || B[i] == A[0]`\n`a` and `b` are the number of swap to make a whole row `A[0]`\n\n2. Try `B[0]`\nthe condition is that `A[i] == B[0] || B[i] == B[0]`\n`a` and `b` are the number of swap to make a whole row `B[0]`\n\n3. Return -1\n\n**Java**\n```java\n    public int minDominoRotations(int[] A, int[] B) {\n        int n = A.length;\n        for (int i = 0, a = 0, b = 0; i < n && (A[i] == A[0] || B[i] == A[0]); ++i) {\n            if (A[i] != A[0]) a++;\n            if (B[i] != A[0]) b++;\n            if (i == n - 1) return Math.min(a, b);\n        }\n        for (int i = 0, a = 0, b = 0; i < n && (A[i] == B[0] || B[i] == B[0]); ++i) {\n            if (A[i] != B[0]) a++;\n            if (B[i] != B[0]) b++;\n            if (i == n - 1) return Math.min(a, b);\n        }\n        return -1;\n    }\n```\n\n**C++**\n```cpp\n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        int n = A.size();\n        for (int i = 0, a = 0, b = 0; i < n && (A[i] == A[0] || B[i] == A[0]); ++i) {\n            if (A[i] != A[0]) a++;\n            if (B[i] != A[0]) b++;\n            if (i == n - 1) return min(a, b);\n        }\n        for (int i = 0, a = 0, b = 0; i < n && (A[i] == B[0] || B[i] == B[0]); ++i) {\n            if (A[i] != B[0]) a++;\n            if (B[i] != B[0]) b++;\n            if (i == n - 1) return min(a, b);\n        }\n        return -1;\n    }\n```\n<br>\n\n## Solution 3\nFind intersection set `s` of all `{A[i], B[i]}`\n`s.size = 0`, no possible result.\n`s.size = 1`, one and only one result.\n`s.size = 2`, it means all dominoes are `[a,b]` or `[b,a]`, try either one.\n`s.size > 2`, impossible.\n\n**Java:**\n```java\n    public int minDominoRotations(int[] A, int[] B) {\n        HashSet<Integer> s = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 6));\n        int[] countA = new int[7], countB = new int[7];\n        for (int i = 0; i < A.length; ++i) {\n            s.retainAll(new HashSet<>(Arrays.asList(A[i], B[i])));\n            countA[A[i]]++;\n            countB[B[i]]++;\n        }\n        for (int i : s)\n            return A.length - Math.max(countA[i], countB[i]);\n        return -1;\n    }\n```\n\n**Python:**\n```py\n    def minDominoRotations(self, A, B):\n        s = reduce(set.__and__, (set(d) for d in zip(A, B)))\n        if not s: return -1\n        x = s.pop()\n        return min(len(A) - A.count(x), len(B) - B.count(x))\n```\n
[]