|
| 1 | +<h2><a href="https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/">1605. Find Valid Matrix Given Row and Column Sums</a></h2><h3>Medium</h3><hr><div><p>You are given two arrays <code>rowSum</code> and <code>colSum</code> of non-negative integers where <code>rowSum[i]</code> is the sum of the elements in the <code>i<sup>th</sup></code> row and <code>colSum[j]</code> is the sum of the elements of the <code>j<sup>th</sup></code> 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.</p> |
| 2 | + |
| 3 | +<p>Find any matrix of <strong>non-negative</strong> integers of size <code>rowSum.length x colSum.length</code> that satisfies the <code>rowSum</code> and <code>colSum</code> requirements.</p> |
| 4 | + |
| 5 | +<p>Return <em>a 2D array representing <strong>any</strong> matrix that fulfills the requirements</em>. It's guaranteed that <strong>at least one </strong>matrix that fulfills the requirements exists.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | + |
| 10 | +<pre><strong>Input:</strong> rowSum = [3,8], colSum = [4,7] |
| 11 | +<strong>Output:</strong> [[3,0], |
| 12 | + [1,7]] |
| 13 | +<strong>Explanation:</strong> |
| 14 | +0<sup>th</sup> row: 3 + 0 = 3 == rowSum[0] |
| 15 | +1<sup>st</sup> row: 1 + 7 = 8 == rowSum[1] |
| 16 | +0<sup>th</sup> column: 3 + 1 = 4 == colSum[0] |
| 17 | +1<sup>st</sup> column: 0 + 7 = 7 == colSum[1] |
| 18 | +The row and column sums match, and all matrix elements are non-negative. |
| 19 | +Another possible matrix is: [[1,2], |
| 20 | + [3,5]] |
| 21 | +</pre> |
| 22 | + |
| 23 | +<p><strong class="example">Example 2:</strong></p> |
| 24 | + |
| 25 | +<pre><strong>Input:</strong> rowSum = [5,7,10], colSum = [8,6,8] |
| 26 | +<strong>Output:</strong> [[0,5,0], |
| 27 | + [6,1,0], |
| 28 | + [2,0,8]] |
| 29 | +</pre> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Constraints:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li><code>1 <= rowSum.length, colSum.length <= 500</code></li> |
| 36 | + <li><code>0 <= rowSum[i], colSum[i] <= 10<sup>8</sup></code></li> |
| 37 | + <li><code>sum(rowSum) == sum(colSum)</code></li> |
| 38 | +</ul> |
| 39 | +</div> |
0 commit comments