leetcode 1605

简介

leetcode 1605
思路:代码抄的,没啥想法
例如一个3*3的矩阵求解,我们已知6个方程,但是这个矩阵有9个变量,如何求解?注定是不能求出唯一解的,如果不是限定在非负整数数组范围内,那么就有无穷多解。
作者使用了贪心的策略,因为这题后面解的变动不会对前面的已经使用贪心策略的解产生变动,而且可以很快求出解,逻辑上可以使用贪心。

参考链接

https://github.com/haoel/leetcode
推荐个人的刷题框架(当然有新的想法我会合并的哈哈哈哈)
https://github.com/lishaohsuai/leetCode

code

#include <vector>
#include <queue>
using namespace std;
class Solution1605{
public:
    vector<vector<int>> restoreMatrix(vector<int>& row, vector<int>& col){
        int n = row.size();
        int m = col.size();
        if(n==0 || m ==0){
            return {};
        }
        vector<vector<int>> res(n, vector<int>(m,0));
        queue<pair<int, int>> p,q;
        for(int i=0; i<n; i++){
            p.push({row[i],i});
        }
        for(int j=0; j<m; j++){
            q.push({col[j],j});
        }

        while(!q.empty() &&!p.empty()){
            auto a = p.front();
            auto b = q.front();
            p.pop();
            q.pop();
            int t = min(a.first, b.first);
            res[a.second][b.second] = t;
            a.first -= t;
            b.first -= t;
            if(a.first > 0)
                p.push(a);
            if(b.first > 0)
                q.push(b);
        }
        return res;
    }
};
原文地址:https://www.cnblogs.com/eat-too-much/p/14244681.html