Find Valid Matrix Given Row and Column Sums - Problem

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

example_1.py โ€” Basic Case
$ Input: rowSum = [3,8], colSum = [4,7]
โ€บ Output: [[3,0],[1,7]]
๐Ÿ’ก Note: We need a 2ร—2 matrix where row sums are [3,8] and column sums are [4,7]. The greedy approach places 3 at (0,0), then 0 at (0,1), then 1 at (1,0), and finally 7 at (1,1). This satisfies all constraints.
example_2.py โ€” Single Element
$ Input: rowSum = [5], colSum = [5]
โ€บ Output: [[5]]
๐Ÿ’ก Note: With only one cell, the value must equal both the row sum and column sum, which are the same (5).
example_3.py โ€” Multiple Solutions
$ Input: rowSum = [2,2], colSum = [2,2]
โ€บ Output: [[2,0],[0,2]]
๐Ÿ’ก Note: There are multiple valid solutions like [[0,2],[2,0]] or [[1,1],[1,1]], but our greedy approach produces [[2,0],[0,2]].

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

Visualization

Tap to expand
$3k$0k$1k$7kIT DeptMarketingApp DevWeb DevTotal: $3k โœ“Total: $8k โœ“Need: $4k โœ“Need: $7k โœ“Greedy Budget Allocation1. IT โ†’ App Dev: min($3k, $4k) = $3k2. IT โ†’ Web Dev: min($0k, $7k) = $0k3. Marketing โ†’ App Dev: min($8k, $1k) = $1k4. Marketing โ†’ Web Dev: min($7k, $7k) = $7k
Understanding the Visualization
1
Setup Budget Constraints
Each department has a total budget (rowSum), each project needs total funding (colSum)
2
Greedy Allocation
For each department-project pair, allocate the minimum of what department has left and what project needs
3
Update Remaining
Subtract allocated amount from both department budget and project requirement
4
Verify Solution
Check that all departments spent their exact budget and all projects got exact funding needed
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because taking min(row_remaining, col_remaining) at each step ensures we never over-allocate while maximizing progress toward satisfying both constraints simultaneously.
Asked in
Google 25 Amazon 18 Microsoft 12 Meta 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen