Find Valid Matrix Given Row and Column Sums LeetCode solution in Python
1class Solution:2def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:3matrix = []4for row in range(len(rowSum)):5data, array = rowSum[row], []6for col in range(len(colSum)):7if data == 0 or colSum[col] == 0:8array.append(0)9elif data > colSum[col]:10data -= colSum[col]11array.append(colSum[col])12colSum[col] = 013else:14array.append(data)15colSum[col] -= data16data = 017matrix.append(array)1819return matrix