Minimum Domino Rotations For Equal Row

Problem Id: 1007 Difficulty: Medium


Intuition

Solution


class Solution:
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        options = [
            self._helper(A, B, A[0]),
            self._helper(B, A, A[0]),
            self._helper(A, B, B[0]),
            self._helper(B, A, B[0])
        ]
        options = [i for i in options if i is not None]
        if not options:
            return -1
        return min(options)

    def _helper(self, A, B, value):
        rotate = 0
        for i in range(len(A)):
            if A[i] == value:
                continue
            elif B[i] == value:
                rotate += 1
            else:
                return None
        return rotate