5518. 给定行和列的和求可行矩阵
贪心算法
class Solution {
public:
vector<vector<int>> restoreMatrix(vector<int>& rowSum, vector<int>& colSum) {
int row = rowSum.size();
int col = colSum.size();
vector<vector<int> > ans(row,vector<int>(col,0));
vector<int> rowNow(row,0);
vector<int> colNow(col,0);
for(auto i = 0;i < row;i++){
for(auto j = 0;j<col;j++){
int now = min(rowSum[i]-rowNow[i],colSum[j]-colNow[j]);
ans[i][j] = now;
rowNow[i] += ans[i][j];
colNow[j] += ans[i][j];
}
}
return ans;
}
};