1605. 给定行和列的和求可行矩阵

leetcode-1605. 给定行和列的和求可行矩阵

一直贪心就行了,自由度很高。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
int m = rowSum.size();
int n = colSum.size();
vector<vector<int>> result = vector(m, vector(n, 0));
int i = 0, j = 0;
while (i < m && j < n)
{
int v = min(rowSum[i], colSum[j]);
result[i][j] = v;
rowSum[i] -= v;
colSum[j] -= v;
if (rowSum[i] == 0) {
++i;
}
if (colSum[j] == 0) {
++j;
}
}
return result;
}
};