Leetcode 1711 Solution

This article provides solution to leetcode question 1711 (find-valid-matrix-given-row-and-column-sums)

https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums

Solution

class Solution:
    def restoreMatrix(self, rowSum: List[int], colSum: List[int]) -> List[List[int]]:
        m = len(rowSum)
        n = len(colSum)

        ans = [[0 for _ in range(n)] for _ in range(m)]

        for i in range(m):
            for j in range(n):
                ans[i][j] = max(0, min(rowSum[i], colSum[j]))
                rowSum[i] -= ans[i][j]
                colSum[j] -= ans[i][j]

        return ans