C++ O(N) 10 lines
`\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        int ans = A.size()+1;\n        for (int i = 1; i <= 6; ++ i)
```\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        int ans = A.size()+1;\n        for (int i = 1; i <= 6; ++ i) {\n            int top = 0, bottom = 0, k = 0;\n            while (k < A.size() && (A[k] == i || B[k] == i)) {\n                if (B[k] != i) ++ bottom;\n                if (A[k] != i) ++ top;\n                ++ k;\n            }\n            if (k == A.size()) ans = min(ans, min(bottom, top));\n        }\n        if (ans == (A.size()+1)) return -1;\n        else return ans;\n    }\n};\n``
[]