DEV Community

Mardonbek
Mardonbek

Posted on

Find Valid Matrix Given Row and Column Sums.

In this code, it is displayed how to restore matrix.
var restoreMatrix = function(rowSum, colSum) {
let m = rowSum.length;
let n = colSum.length;
let matrix = new Array(m).fill(0).map(() => new Array(n).fill(0));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
matrix[i][j] = Math.min(rowSum[i], colSum[j]);
rowSum[i] -= matrix[i][j];
colSum[j] -= matrix[i][j];
}
}
return matrix;

};

Top comments (0)