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;
    }
};
posted @ 2020-10-04 00:54  是水泵呢  阅读(151)  评论(0编辑  收藏  举报