Imagine you're working as a data analyst for a company that tracks sales across different regions and product categories. You have the total sales for each region (row sums) and the total sales for each product category (column sums), but the original detailed sales matrix has been lost!
Your task is to reconstruct any valid sales matrix that matches these constraints. You need to find a matrix of non-negative integers where:
- Each row sums to the corresponding value in
rowSum - Each column sums to the corresponding value in
colSum - All matrix elements are โฅ 0 (you can't have negative sales!)
The good news? It's guaranteed that at least one valid solution exists, so you don't need to worry about impossible cases. You just need to find any matrix that works!
Goal: Return a 2D matrix of size rowSum.length ร colSum.length that satisfies all the given row and column sum constraints.
Input & Output
Constraints
- 1 โค rowSum.length, colSum.length โค 500
- 0 โค rowSum[i], colSum[i] โค 108
- sum(rowSum) == sum(colSum)
- It's guaranteed that at least one matrix exists