Участник:Лукьянов Кирилл/Reconstruct a 2-Row Binary Matrix

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

https://leetcode.com/problems/reconstruct-a-2-row-binary-matrix

  class Solution:
    def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]:
        n = len(colsum)
        sol = [[0 for j in range(n)] for i in range(2)]
        su = upper
        sl = lower
        s = upper + lower
        for i in range(n):
            s -= colsum[i]
            if(colsum[i] == 2):
                sol[0][i] = 1
                sol[1][i] = 1
                upper -= 1
                lower -= 1
                su -= 1
                sl -= 1
            elif(colsum[i] == 0):
                sol[0][i] = 0
                sol[1][i] = 0
        for i in range(n):
            if(upper > 0 and sol[0][i] == 0 and sol[1][i] == 0 and colsum[i] == 1):
                sol[0][i] = 1
                sol[1][i] = 0   
                upper -= 1
                su -= 1
            elif(lower > 0 and sol[0][i] == 0 and sol[1][i] == 0 and colsum[i] == 1):
                sol[0][i] = 0
                sol[1][i] = 1
                lower -= 1
                sl -= 1
        if (s == 0 and su == 0 and sl == 0):
            return sol
        else:
            return []