Участник:Лукьянов Кирилл/Minimum Domino Rotations For Equal Row

Материал из DISCOPAL
Перейти к: навигация, поиск


https://leetcode.com/problems/minimum-domino-rotations-for-equal-row/

 
class Solution:
 
    def minDominoRotations(self, A: List[int], B: List[int]) -> int:
        n = len(A)
        flag = 0
        focus = 0
        kol_eq = 0
        err = 0
        for i, A[i] in enumerate(A):
            if A[i] == B[i]:
                flag = 2
                focus = A[i]
                kol_eq += 1
        if flag != 2:
            focus = A[0]
            for i, A[i] in enumerate(A):
                if A[i] != focus and B[i] != focus:
                    err += 1
            if err != 0:
                err = 0
                focus = B[0]
                for i, A[i] in enumerate(A):
                    if A[i] != focus and B[i] != focus:
                        err += 1
            # print(err)
        elif flag == 2:
            for i, A[i] in enumerate(A):
                if(A[i] != focus and B[i] != focus):
                    err = 1
        if err > 0:
            focus = 0
        kol = 0
        if focus == 0:
            return -1
        else:
            for i, A[i] in enumerate(A):
                if A[i] == focus:
                    kol += 1
            kol -= kol_eq
            n -= kol_eq
            if kol <= n - kol:
                return kol
            else:
                return n - kol