2024-07-20
1605. Find Valid Matrix Given Row and Column Sums
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.
偷看解答.jpg
用 greedy 从左上开始填
让每次填格都能直接消掉一个 sum 条件
也就是 rowSum, colSum 选比较小的填进去
就可以消掉一个条件
另一个扣掉对应数字以后继续往下走填格