Python O(len(A)) one pass
\nclass Solution(object):\n    def minDominoRotations(self, A, B):\n        """\n        :type A: List[int]\n        :type B: List[int]\n        :rtype: int\n  
```\nclass Solution(object):\n    def minDominoRotations(self, A, B):\n        """\n        :type A: List[int]\n        :type B: List[int]\n        :rtype: int\n        """\n        \n        \n        L = len(A)\n        p1,p2 = A[0],B[0] \n        # if valid result exists, the row shold have value p1 or p2\n        dp1 = dp2 = c1 = c2 =0 \n        # dp1 represents the number of A[i] == B[i] == p1\n        # dp2 represents the number of A[i] == B[i] == p2\n        # c1 stores the numer of swaps needed such that the upper row has the same value\n        # c2 stores the numer of swaps needed such that the lower row has the same value\n        \n        for i in xrange(1,L):\n            if A[i] not in [p1,p2] and B[i] not in [p1,p2]: return -1\n            \n            if p1 not in [A[i],B[i]]: \n                p1 = 0\n            else:\n                if p1 == A[i] == B[i]: dp1 += 1\n                elif p1!=A[i]: c1 += 1\n            \n            if p2 not in [A[i],B[i]]: \n                p2 = 0\n            else:\n                if p2 == B[i] == A[i]: dp2 += 1\n                elif p2!=B[i]: c2 += 1\n            \n            if not p1 and not p2: return -1\n        \n        v1 = v2 = float(\'inf\')\n        if p1: v1 = min(c1,L-c1-dp1)\n        if p2: v2 = min(c2,L-c2-dp2)\n        \n        return  min(v1,v2)\n\t\t\n\t\t# there should be ways to optimize this code, any suggesion is appreciated\n        \n\n```
[]