matrix
4 years ago in Python
class Solution:
def reconstructMatrix(self, upper: int, lower: int, colsum: List[int]) -> List[List[int]]:
l = len(colsum)
if upper + lower != sum(colsum):
return []
num_two = colsum.count(2)
if num_two > lower or num_two > upper:
return []
matrix = [[-1] * l , [-1] * l]
to_resolve = []
for i, col in enumerate(colsum):
if col == 0:
matrix[0][i] = 0
matrix[1][i] = 0
if col == 2:
matrix[0][i] = 1
matrix[1][i] = 1
if col == 1:
to_resolve.append(i)
diffs = lower - upper
for r in to_resolve:
if diffs > 0:
matrix[0][r] = 0
matrix[1][r] = 1
diffs -= 1
else:
matrix[0][r] = 1
matrix[1][r] = 0
diffs += 1
return matrix