# 10. Miscelleaneous problems

### 1007. Minimum Domino Rotations For Equal Row

In a row of dominoes, tops[i] and bottoms[i] represent the top and bottom halves of the ith domino. (A domino is a tile with two numbers from 1 to 6 - one on each half of the tile.)

We may rotate the ith domino, so that tops[i] and bottoms[i] swap values.

Return the minimum number of rotations so that all the values in tops are the same, or all the values in bottoms are the same.

If it cannot be done, return -1.

Example:

Input: tops = [2,1,2,4,2,2], bottoms = [5,2,6,2,3,2] > Output: 2 Explanation: The first figure represents the dominoes as given by tops and bottoms: before we do any rotations.
If we rotate the second and fourth dominoes, we can make every value in the top row equal to 2, as indicated by the second figure.

code

from collections import Counter

class Solution:
def minDominoRotations(self, tops: List[int], bottoms: List[int]) -> int:

def minRotationWithRef(arr: List[int], swap:List[int]) -> int:
"""Returns a minimum rotations to normalize arr"""

arrCount = Counter(tops)
arrCount = dict(sorted(arrCount.items(), key=lambda x:x[1], reverse=True))

minTurns = float("inf")

for ref, v in arrCount.items():

turns = 0

for i in range(len(arr)):

if arr[i] == ref:
continue

if arr[i] != ref and swap[i] != ref:
break

turns += 1

if i == len(arr)-1:
minTurns = min(minTurns, turns)

return minTurns

minRotateTops = minRotationWithRef(tops, bottoms)
minRotateBottom = minRotationWithRef(bottoms, tops)

ans = min(minRotateTops, minRotateBottom)

if ans == float("inf"):
return -1

return ans