📌  相关文章
📜  用给定和的每一行和每一列生成一个矩阵

📅  最后修改于: 2021-09-08 12:40:33             🧑  作者: Mango

给定大小分别为NM 的两个数组row[]col[] ,任务是构造一个维度为N × M的矩阵,使得每i 行中矩阵元素的总和为row[i]并且在每一个j列的矩阵元素是COL [J]。

例子:

方法:解决这个问题最简单的方法是使用贪心方法。请按照以下步骤解决此问题:

  • 初始化一个维度为N × M的矩阵。
  • 按以下方式开始填充矩阵的每个单元格(i, j)
    • 对于每个单元格(i, j) ,选择row[i]col[j]的最小值,并将其放置在单元格(i, j) 处。让它最小
    • row[i]col[j] 中减去minm
  • 完成上述步骤后,打印形成的矩阵。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to generate a matrix with
// sum of each row equal to sum of r[]
// and sum of each column equal to sum of c[]
vector > restoreGem(
    vector& r, vector& c)
{
    // Initialize a matrix
    vector > dp(r.size(),
                            vector(c.size(), 0));
 
    // Traverse each cell (i, j) of the matrix
    for (int i = 0; i < r.size(); i++) {
        for (int j = 0; j < c.size(); j++) {
 
            // Assign the minimum of the
            // row or column value
            int m = min(r[i], c[j]);
            dp[i][j] = m;
 
            // Subtract the minimum from
            // both row and column sum
            r[i] -= m;
            c[j] -= m;
        }
    }
 
    return dp;
}
 
void printMatrix(vector > ans,
                 int N, int M)
{
 
    // Print the matrix obtained
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    vector rowSum = { 5, 7, 10 };
    vector colSum = { 8, 6, 8 };
 
    vector > ans
        = restoreGem(rowSum, colSum);
 
    printMatrix(ans, rowSum.size(),
                colSum.size());
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
// Function to generate a matrix with
// sum of each row equal to sum of r[]
// and sum of each column equal to sum of c[]
static int[][] restoreGem(int[] r, int[] c)
{
   
    // Initialize a matrix
    int[][] dp = new int[r.length][c.length];
 
    // Traverse each cell (i, j) of the matrix
    for (int i = 0; i < r.length; i++)
    {
        for (int j = 0; j < c.length; j++)
        {
 
            // Assign the minimum of the
            // row or column value
            int m = Math.min(r[i], c[j]);
            dp[i][j] = m;
 
            // Subtract the minimum from
            // both row and column sum
            r[i] -= m;
            c[j] -= m;
        }
    }
    return dp;
}
 
static void printMatrix(int[][] ans,
                 int N, int M)
{
 
    // Print the matrix obtained
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            System.out.print(ans[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int[] rowSum = { 5, 7, 10 };
    int[] colSum = { 8, 6, 8 };
 
    int[][] ans
        = restoreGem(rowSum, colSum);
 
    printMatrix(ans, rowSum.length,
                colSum.length);
}
}
 
// This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
using System;
public class GFG
{
 
// Function to generate a matrix with
// sum of each row equal to sum of r[]
// and sum of each column equal to sum of c[]
static int[,] restoreGem(int[] r, int[] c)
{
   
    // Initialize a matrix
    int[,] dp = new int[r.Length, c.Length];
 
    // Traverse each cell (i, j) of the matrix
    for (int i = 0; i < r.Length; i++)
    {
        for (int j = 0; j < c.Length; j++)
        {
 
            // Assign the minimum of the
            // row or column value
            int m = Math.Min(r[i], c[j]);
            dp[i,j] = m;
 
            // Subtract the minimum from
            // both row and column sum
            r[i] -= m;
            c[j] -= m;
        }
    }
    return dp;
}
 
static void printMatrix(int[,] ans,
                 int N, int M)
{
 
    // Print the matrix obtained
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
            Console.Write(ans[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] rowSum = { 5, 7, 10 };
    int[] colSum = { 8, 6, 8 };
 
    int[,] ans
        = restoreGem(rowSum, colSum);
    printMatrix(ans, rowSum.Length,
                colSum.Length);
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
5 0 0 
3 4 0 
0 2 8

时间复杂度: O(N×M)
辅助空间: O(N×M)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live