Skip to main content
Back to LeetCode Archive
Jul 20, 20245 min read

1605. Find Valid Matrix Given Row and Column Sums (20 July 2024)

ArrayMatrixGreedy

Problem Description

Given two arrays rowSum and colSum representing the sum of elements in each row and column of a matrix, return any matrix satisfying these row and column sums.

A valid matrix should have dimensions equal to the lengths of rowSum and colSum, and the sums of its rows and columns should match the given rowSum and colSum vectors.

You can find the problem statement on LeetCode.

Solution Explanation

To solve this problem, we need to construct a matrix such that the sums of its rows and columns match the given vectors rowSum and colSum. Here’s a step-by-step explanation of how we can achieve this:

1. Initialize the Matrix

  • Create a matrix with dimensions equal to the lengths of rowSum and colSum, initialized to zero.

2. Iterate Through the Matrix

  • Iterate through each cell of the matrix.
  • For each cell (i, j), assign it the minimum value of the remaining sums of the respective row and column: matrix[i][j] = min(rowSum[i], colSum[j]).

3. Update the Sums

  • After assigning the value, subtract this value from both rowSum[i] and colSum[j].

4. Repeat Until Complete

  • Continue the process until all cells have been filled, ensuring that all row and column sums are reduced to zero.

Here is the C++ code implementing this solution:

class Solution {
public:
    vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
        int rowSize = rowSum.size();
        int colSize = colSum.size();
        vector<vector<int>> mat(rowSize, vector<int>(colSize, 0));
 
        for (int i = 0; i < rowSize; ++i) {
            for (int j = 0; j < colSize; ++j) {
                mat[i][j] = min(rowSum[i], colSum[j]);
                rowSum[i] -= mat[i][j];
                colSum[j] -= mat[i][j];
            }
        }
 
        return mat;
    }
};

Proof

Initialization and Correctness of Assignment

  1. Start with the original row sums and column sums.
  2. For each cell mat[i][j], assign the minimum of the remaining row and column sums:
    mat[i][j] = min(rowSum[i], colSum[j]);
    This assignment ensures that no row or column sum is exceeded.

Reduction of Sums

  1. After assigning mat[i][j] = min(rowSum[i], colSum[j]):
  2. Subtract this value from both rowSum[i] and colSum[j].
  3. This step ensures that the remaining sums for row i and column j are correctly updated.

Guaranteeing Zero Row and Column Sums

  1. Since each cell assignment reduces the respective row and column sums by the same amount, the sums will correctly decrease to zero.
  2. By the end of the process, all row sums and column sums will be zero, ensuring that the conditions ∑j mat[i][j] = rowSum[i] and ∑i mat[j][i] = colSum[j] are met.

Completeness

  1. The approach iterates over every cell in the matrix, ensuring all positions are filled.
  2. Each iteration uses the minimum possible value, ensuring that both row and column constraints are simultaneously satisfied.

Termination

  1. The algorithm terminates after filling all cells, with each cell reducing the respective row and column sums towards zero.
  2. Since the sums are finite and positive, the process will complete after rowSize × colSize iterations.

Mathematical Formulation

Initialization

∀i, j mat[i][j] = 0, rowSum[i] ≥ 0, colSum[j] ≥ 0;

Assignment

mat[i][j] = min(rowSum[i], colSum[j]);

Update

rowSum[i] ← rowSum[i] − mat[i][j];
colSum[j] ← colSum[j] − mat[i][j];

Conclusion

The approach ensures that each cell assignment is made in a way that satisfies the constraints of the problem by iteratively reducing the row and column sums until they are all zero. The steps ensure correctness and completeness by making minimal assignments that respect the required sums. The algorithm operates in O(rowSize × colSize) time, making it efficient for the given problem constraints.