Участник:Лукьянов Кирилл/Score After Flipping Matrix

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


https://leetcode.com/problems/score-after-flipping-matrix

 
class Solution:
 
    def matrixScore(self, A: List[List[int]]) -> int:
        kol0 = 0
        n = len(A)
        m = len(A[0])
        for i in range(n):
            if A[i][0] == 0:
                for j in range(m):
                    if A[i][j] == 0:
                        A[i][j] = 1
                    else:
                        A[i][j] = 0
 
        for j in range(m):
            kol0 = 0
            for i in range(n):
                if A[i][j] == 0:
                    kol0 += 1
            if kol0 > n - kol0:
                for i in range(n):
                    if A[i][j] == 0:
                        A[i][j] = 1
                    else:
                        A[i][j] = 0
 
        sum = 0
        k = 1
        for j in range(m - 1, -1, -1):
            for i in range(n):
               sum += A[i][j] * k
            k *= 2
        return sum