You are given two arrays rowSum and colSum of non-negative integers where rowSum[i] is the sum of the elements in the ith row and colSum[j] is the sum of the elements of the jth column of a 2D matrix. In other words, you do not know the elements of the matrix, but you do know the sums of each row and column.
Find any matrix of non-negative integers of size rowSum.length x colSum.length that satisfies the rowSum and colSum requirements.
Return a 2D array representing any matrix that fulfills the requirements. It's guaranteed that at least one matrix that fulfills the requirements exists.
Example 1:
Input: rowSum = [3,8], colSum = [4,7]
Output: [[3,0],
[1,7]]
Explanation:
0th row: 3 + 0 = 3 == rowSum[0]
1st row: 1 + 7 = 8 == rowSum[1]
0th column: 3 + 1 = 4 == colSum[0]
1st column: 0 + 7 = 7 == colSum[1]
The row and column sums match, and all matrix elements are non-negative.
Another possible matrix is: [[1,2],
[3,5]]
Example 2:
Input: rowSum = [5,7,10], colSum = [8,6,8]
Output: [[0,5,0],
[6,1,0],
[2,0,8]]
Constraints:
1 <= rowSum.length, colSum.length <= 5000 <= rowSum[i], colSum[i] <= 108sum(rowSum) == sum(colSum)When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method tries every possible combination to fill the matrix. We systematically explore all potential values for each cell, making sure row and column sum constraints are met at the end. If a combination does not satisfy all the criteria, it's discarded.
Here's how the algorithm would work step-by-step:
def find_valid_matrix_brute_force(row_sums, col_sums):
number_of_rows = len(row_sums)
number_of_columns = len(col_sums)
matrix = [[0] * number_of_columns for _ in range(number_of_rows)]
def backtrack(row_index, col_index):
if row_index == number_of_rows:
#Check matrix validity
actual_row_sums = [sum(row) for row in matrix]
actual_col_sums = [sum(matrix[i][j] for i in range(number_of_rows)) for j in range(number_of_columns)]
if actual_row_sums == row_sums and actual_col_sums == col_sums:
return True
else:
return False
next_row_index = row_index
next_col_index = col_index + 1
if next_col_index == number_of_columns:
next_row_index = row_index + 1
next_col_index = 0
# Try every possible value for the current cell
for possible_value in range(min(row_sums[row_index], col_sums[col_index]) + 1):
matrix[row_index][col_index] = possible_value
# Recursively try to fill the rest of the matrix
if backtrack(next_row_index, next_col_index):
return True
# Reset the cell for backtracking purposes
matrix[row_index][col_index] = 0
return False
if backtrack(0, 0):
return matrix
else:
return []The core idea is to fulfill the row and column sum requirements one cell at a time in a greedy fashion. We iteratively find the smallest requirement of the current row and column and assign that value to the corresponding cell, effectively satisfying either the row or column constraint (or both) and reducing the problem size.
Here's how the algorithm would work step-by-step:
def find_valid_matrix(row_sums, column_sums):
number_of_rows = len(row_sums)
number_of_columns = len(column_sums)
result_matrix = [[0] * number_of_columns for _ in range(number_of_rows)]
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
# We take the minimum to satisfy at least one constraint.
current_value = min(row_sums[row_index], column_sums[column_index])
result_matrix[row_index][column_index] = current_value
# Update the row and column sums after assigning the value.
row_sums[row_index] -= current_value
column_sums[column_index] -= current_value
return result_matrix| Case | How to Handle |
|---|---|
| Empty rowSum or colSum arrays | Return an empty matrix (or null) as there's no valid output possible. |
| rowSum and colSum have different total sums | Return an empty matrix (or null) because a valid matrix cannot be constructed. |
| Single row and single column (1x1 matrix) | Create a 1x1 matrix where the single element is the value from both rowSum[0] and colSum[0] (which must be equal). |
| rowSum or colSum contains negative numbers | The problem states non-negative integers only, so return an error or IllegalArgumentException. |
| Large rowSum and colSum values leading to integer overflow | Use a larger integer type (e.g., long) to prevent overflow during assignment to the matrix. |
| All rowSum and colSum values are zero | Create a matrix filled with zeros. |
| Only one rowSum or colSum is non-zero while the others are zero | Allocate all the value to one cell on that row or colum. |
| Multiple valid solutions exist | The algorithm should produce *a* valid solution; any valid solution is acceptable. |