Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SuryaPratapK/a6539db8d600d99756afe4566543bec8 to your computer and use it in GitHub Desktop.
Save SuryaPratapK/a6539db8d600d99756afe4566543bec8 to your computer and use it in GitHub Desktop.
class Solution {
int tryRotationMatching(int number,vector<int>& first,vector<int>& second){
int count = 0;
for(int i=0;i<first.size();++i){
if(first[i]==number) continue;
else if(second[i]==number) count++;
else return INT_MAX;
}
return count;
}
public:
int minDominoRotations(vector<int>& tops, vector<int>& bottoms) {
int min_rotations = INT_MAX;
for(int number = 1; number<=6; number++){
int count_tops = tryRotationMatching(number,tops,bottoms);
int count_bottoms = INT_MAX;
if(count_tops!=INT_MAX)
count_bottoms = tryRotationMatching(number,bottoms,tops);
min_rotations = min(min_rotations,min(count_tops,count_bottoms));
}
return min_rotations==INT_MAX? -1: min_rotations;
}
};
/*
//JAVA
import java.util.*;
class Solution {
private int tryRotationMatching(int number, int[] first, int[] second) {
int count = 0;
for (int i = 0; i < first.length; ++i) {
if (first[i] == number) continue;
else if (second[i] == number) count++;
else return Integer.MAX_VALUE;
}
return count;
}
public int minDominoRotations(int[] tops, int[] bottoms) {
int minRotations = Integer.MAX_VALUE;
for (int number = 1; number <= 6; number++) {
int countTops = tryRotationMatching(number, tops, bottoms);
int countBottoms = Integer.MAX_VALUE;
if (countTops != Integer.MAX_VALUE) {
countBottoms = tryRotationMatching(number, bottoms, tops);
}
minRotations = Math.min(minRotations, Math.min(countTops, countBottoms));
}
return minRotations == Integer.MAX_VALUE ? -1 : minRotations;
}
}
#Python
class Solution:
def tryRotationMatching(self, number: int, first: List[int], second: List[int]) -> int:
count = 0
for i in range(len(first)):
if first[i] == number:
continue
elif second[i] == number:
count += 1
else:
return float('inf')
return count
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
min_rotations = float('inf')
for number in range(1, 7):
count_tops = self.tryRotationMatching(number, tops, bottoms)
count_bottoms = float('inf')
if count_tops != float('inf'):
count_bottoms = self.tryRotationMatching(number, bottoms, tops)
min_rotations = min(min_rotations, count_tops, count_bottoms)
return min_rotations if min_rotations != float('inf') else -1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment