[leetcode]Minimum Domino Rotations For Equal Row

贪心,只要检查第一个骨牌上下是否能满足即可

class Solution:
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        if len(A) == 0:
            return -1
        
        def checkUpDown(A, B):
            n = len(A)
            cntUp, cntDown = 0, 0
            for i in range(n):
                if cntUp != float('inf') and A[i] != A[0]:
                    if B[i] == A[0]:
                        cntUp += 1
                    else:
                        cntUp = float('inf')
                if cntDown != float('inf') and B[i] != A[0]:
                    if A[i] == A[0]:
                        cntDown += 1
                    else:
                        cntDown = float('inf')
            return cntUp, cntDown

        cntAUp, cntADown = checkUpDown(A, B)
        cntBUp, cntBDown = checkUpDown(B, A)

        maxAB = min(cntAUp, cntADown, cntBUp, cntBDown)
        if maxAB == float('inf'):
            return -1
        else:
            return maxAB
            

  

你可能感兴趣的:([leetcode]Minimum Domino Rotations For Equal Row)