1605. Find Valid Matrix Given Row and Column Sums

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1605

class Solution:
    def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
        m, n = len(rowSum), len(colSum)
        res = [[0] * n for _ in range(m)]
        for i in range(m):
            for j in range(n):
                x = min(rowSum[i], colSum[j])
                res[i][j] = x
                rowSum[i] -= x
                colSum[j] -= x
        return res