原文链接: https://leetcode-cn.com/problems/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.
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]]
Example 3:
Input: rowSum = [14,9], colSum = [6,9,8] Output: [[0,9,5], [6,0,3]]
Example 4:
Input: rowSum = [1,0], colSum = [1] Output: [[1], [0]]
Example 5:
Input: rowSum = [0], colSum = [0] Output: [[0]]
Constraints:
1 <= rowSum.length, colSum.length <= 500
0 <= rowSum[i], colSum[i] <= 108
sum(rows) == sum(columns)
中文题目
给你两个非负整数数组 rowSum
和 colSum
,其中 rowSum[i]
是二维矩阵中第 i
行元素的和, colSum[j]
是第 j
列元素的和。换言之你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。
请找到大小为 rowSum.length x colSum.length
的任意 非负整数 矩阵,且该矩阵满足 rowSum
和 colSum
的要求。
请你返回任意一个满足题目要求的二维矩阵,题目保证存在 至少一个 可行矩阵。
示例 1:
输入:rowSum = [3,8], colSum = [4,7] 输出:[[3,0], [1,7]] 解释: 第 0 行:3 + 0 = 3 == rowSum[0] 第 1 行:1 + 7 = 8 == rowSum[1] 第 0 列:3 + 1 = 4 == colSum[0] 第 1 列:0 + 7 = 7 == colSum[1] 行和列的和都满足题目要求,且所有矩阵元素都是非负的。 另一个可行的矩阵为:[[1,2], [3,5]]
示例 2:
输入:rowSum = [5,7,10], colSum = [8,6,8] 输出:[[0,5,0], [6,1,0], [2,0,8]]
示例 3:
输入:rowSum = [14,9], colSum = [6,9,8] 输出:[[0,9,5], [6,0,3]]
示例 4:
输入:rowSum = [1,0], colSum = [1] 输出:[[1], [0]]
示例 5:
输入:rowSum = [0], colSum = [0] 输出:[[0]]
提示:
1 <= rowSum.length, colSum.length <= 500
0 <= rowSum[i], colSum[i] <= 108
sum(rows) == sum(columns)
通过代码
高赞题解
解题思路
将第$i$行第$j$列设为$\min(row[i], col[j])$,同时更新$row[i]$和$col[j]$即可。
为什么这一贪心策略是正确的呢?
其实很简单。我们首先考虑第一行,显然有$row[0]\leq\sum_j col[j]$,因此在经过上述操作后,一定能使得$row[0]=0$。同时,因为每次我们取得是$\min(row[0], col[j])$,所以操作后,一定仍满足$\forall j,col[j]\geq0$。这样,我们就把原问题变成了$N-1$行,$M$列的新问题。依次类推,我们就一定能够得到一组可行解。
时间复杂度$O(NM)$。
代码
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
int n = rowSum.size(), m = colSum.size();
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i][j] = min(rowSum[i], colSum[j]);
rowSum[i] -= ans[i][j];
colSum[j] -= ans[i][j];
}
}
return ans;
}
};
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
3689 | 4999 | 73.8% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|