Runtime: 1136 ms, faster than 92.42% of Python3 online submissions for Minimum Domino Rotations For Equal Row.
Kindly Upvote If you Like It
from collections import Counter
class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:
x= Counter(tops).most_common(1)
a=x[0][0] #Finding The Most Common Element In Tops
ct1=x[0][1] #Finding The occurences Of This Element In Tops
y = Counter(bottoms).most_common(1)
b=y[0][0] #Finding The Most Common Element In Bottoms
ct2=y[0][1] #Finding The occurences Of This Element In Bottoms
# We Will Select Between The Two On Basis Of Which Has The Most occurences For the Element To be Swapped
count=0
if ct1>=ct2:
for i in range(len(tops)):
if tops[i]!=a: # Wherever We Dont Get that Element We Will Replace That
tops[i]=bottoms[i]
count+=1
return count if tops.count(tops[0])==len(tops) else -1
#If We Get Identical List Of Elements We Will return Count else return -1
else:
for i in range(len(bottoms)):
if bottoms[i]!=b: # Wherever We Dont Get that Element We Will Replace That
bottoms[i]=tops[i]
count+=1
return count if bottoms.count(bottoms[0])==len(bottoms) else -1