Skip to content

1605. Find Valid Matrix Given Row and Column Sums #76

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

To solve this problem, we can follow these steps:

  1. Start with an empty matrix of size rowSum.length x colSum.length initialized with zeros.
  2. Iterate over the matrix and at each position (i, j), place the minimum value between rowSum[i] and colSum[j] to ensure non-negative values.
  3. Update rowSum[i] and colSum[j] by subtracting the placed value.
  4. Continue until all elements of rowSum and colSum are zero.

Let's implement this solution in PHP: 1605. Find Valid Matrix Given Row and Column Sums

<?PHP

function restoreMatrix($rowSum, $colSum) {
    $m = count($rowSum);
    $n = count($colSum);
    $matrix = array_fill(0, $m, array_fill(0, $n, 0));

    for ($i = 0; $i < $m; $i++) {
        for ($j = 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
1 participant