LeetCode 1007 Minimum Domino Rotations For Equal Row (Python)

Posted by 小明MaxMing on October 19, 2020

题目

In a row of dominoes, A[i] and B[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 A[i] and B[i] swap values.

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

If it cannot be done, return -1.

解题思路

如果答案存在,那么答案一定是第一个骨牌上面的数或者下面的数,分别计算把所有骨牌都转成和这两个数在上面相同或者在下面相同,所需的旋转次数,返回最小值

代码

class Solution:
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        def check(n):
            reta = retb = 0
            for i in range(len(A)):
                if A[i] != n and B[i] != n:
                    return -1
                if A[i] != n:
                    reta += 1
                elif B[i] != n:
                    retb += 1
            return min(reta, retb)
        
        res = check(A[0])
        if res != -1 or A[0] == B[0]:
            return res
        return check(B[0])

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅