[C++] One pass algorithm with explanation,O(1) space,faster than 90.5%,less memory usage than 92.2%
Logic is simple,we need to know how many occurences of some value there are in A,in B,in A union B,after that,pass through all possible values.\nFor any value,l
Logic is simple,we need to know how many occurences of some value there are in A,in B,in A union B,after that,pass through all possible values.\nFor any value,look if in each column we have that value,if we have,its possible to make A or B have only this value,but now,how to know if its better to rotate dominos in array B,or A.\nSimple,subtract union occurences and occurences only in one array.\nSo for value x,minimum rotations is smaller difference.\n\nSorry for bad english,but code is self explainatory.\n\n```\nclass Solution {\npublic:\n    int minDominoRotations(vector<int>& A, vector<int>& B) {\n        int cntsB[7] {0};\n        int cntsA[7]{0};\n        int cnts[7]{0};\n        int n = A.size();\n        for (int i = 0;i<n;i++) {\n            cntsA[A[i]]++;\n            cntsB[B[i]]++;\n            cnts[A[i]]++;\n            if (A[i]!=B[i])cnts[B[i]]++;\n        }\n        int max = INT_MAX;\n        for (int i = 1;i<=6;i++) {\n            if (cnts[i]<n) continue;\n            max = min(max,min(cnts[i]-cntsA[i],cnts[i]-cntsB[i]));\n        }\n        return max==INT_MAX?-1:max;\n        \n    }\n};\n```
[]