Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lctseng/c315ea838a2789fd054b1081d3c4f250 to your computer and use it in GitHub Desktop.
Save lctseng/c315ea838a2789fd054b1081d3c4f250 to your computer and use it in GitHub Desktop.
class Solution {
public:
int minDominoRotations(vector<int>& A, vector<int>& B) {
int count[7] = {0};
for(int i =0;i < size(A);i++){
if(A[i] == B[i]){
count[A[i]]++;
}
else{
count[A[i]]++;
count[B[i]]++;
}
}
// check enough?
int minRoll = INT_MAX;
for(int i = 1;i<=6;i++){
// at most 2 numbers satisfy this condition
if(count[i] == A.size()){
// try roll A to i
int aCount = 0;
for(int j = 0;j < A.size();j++){
if(A[j] != i) aCount++;
}
// try roll B to i
int bCount = 0;
for(int j = 0;j < A.size();j++){
if(B[j] != i) bCount++;
}
minRoll = min(aCount, bCount);
}
}
return minRoll == INT_MAX ? -1 : minRoll;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment