📌  相关文章
📜  Matrix值的最大增加,以保持最大行数和列数不变

📅  最后修改于: 2021-05-14 00:18:39             🧑  作者: Mango

给定维度为M * N的矩阵mat [] [] ,任务是发现必须为每个像元增加总的最大可能值(例如mat [i] [j] ),以使第i行和第j列的最大元素保持不变。

例子:

方法:

  • 沿着给定矩阵的每一行和每一列遍历以存储每一行和每一列的最大值。
  • 遍历给定的矩阵mat [] []和每个单元格(例如mat [i] [j] ),并沿其对应的行和列将当前元素与最大值最小值之间的差相加为:
  • 在上述操作中添加的总值是必需的结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum increment
// in each cell of the given matrix such
// that maximum and minimum value remains
// unaffected
int maxIncrease(vector >& matrix)
{
    // Get the row of matrix as M
    int M = matrix.size();
 
    // Get the column of matrix as N
    int N = matrix[0].size();
 
    // To store the row-wise
    // maximum values
    vector maxRowVal(M, 0);
 
    // To store the column-wise
    // maximum values
    vector maxColVal(N, 0);
 
    // Traverse along the matrix
    // to store the maximum values
    // of each row and column
    for (int i = 0; i < M; i++) {
 
        for (int j = 0; j < N; j++) {
 
            // Store the row-wise
            // maximum values
            maxRowVal[i] = max(maxRowVal[i],
                               matrix[i][j]);
 
            // Store the column-wise
            // maximum values
            maxColVal[j] = max(maxColVal[j],
                               matrix[i][j]);
        }
    }
 
    // Calculate the total amount
    // of increment
    int totalIncrease = 0;
 
    // Traverse matrix mat[][]
    for (int i = 0; i < M; i++) {
 
        for (int j = 0; j < N; j++) {
 
            // The maximum possible
            // amount to increase
            int needToIncrease
                = min(maxRowVal[i],
                      maxColVal[j])
                  - matrix[i][j];
 
            // Total increased value
            totalIncrease += needToIncrease;
        }
    }
 
    // Return the total
    // increased value
    return totalIncrease;
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > matrix{ { 3, 0, 8, 4 },
                                 { 2, 4, 5, 7 },
                                 { 9, 2, 6, 3 },
                                 { 0, 3, 1, 0 } };
 
    // Function Call
    cout << maxIncrease(matrix)
         << endl;
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function to find the maximum increment
// in each cell of the given matrix such
// that maximum and minimum value remains
// unaffected
static int maxIncrease(int [][] matrix)
{
    // Get the row of matrix as M
    int M = matrix.length;
 
    // Get the column of matrix as N
    int N = matrix[0].length;
 
    // To store the row-wise
    // maximum values
    int []maxRowVal = new int[M];
 
    // To store the column-wise
    // maximum values
    int []maxColVal = new int[N];
     
    // Traverse along the matrix
    // to store the maximum values
    // of each row and column
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
 
            // Store the row-wise
            // maximum values
            maxRowVal[i] = Math.max(maxRowVal[i],
                                    matrix[i][j]);
 
            // Store the column-wise
            // maximum values
            maxColVal[j] = Math.max(maxColVal[j],
                                    matrix[i][j]);
        }
    }
 
    // Calculate the total amount
    // of increment
    int totalIncrease = 0;
 
    // Traverse matrix mat[][]
    for (int i = 0; i < M; i++)
    {
        for (int j = 0; j < N; j++)
        {
 
            // The maximum possible
            // amount to increase
            int needToIncrease = Math.min(maxRowVal[i],
                                          maxColVal[j]) -
                                          matrix[i][j];
 
            // Total increased value
            totalIncrease += needToIncrease;
        }
    }
 
    // Return the total
    // increased value
    return totalIncrease;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given matrix
    int [][] matrix = { { 3, 0, 8, 4 },
                        { 2, 4, 5, 7 },
                        { 9, 2, 6, 3 },
                        { 0, 3, 1, 0 } };
 
    // Function Call
    System.out.print(maxIncrease(matrix) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 program for the above approach
 
# Function to find the maximum increment
# in each cell of the given matrix such
# that maximum and minimum value remains
# unaffected
def maxIncrease(matrix):
     
    # Get the row of matrix as M
    M = len(matrix)
 
    # Get the column of matrix as N
    N = len(matrix[0])
 
    # To store the row-wise
    # maximum values
    maxRowVal = [0] * M
 
    # To store the column-wise
    # maximum values
    maxColVal = [0] * (N)
 
    # Traverse along the matrix
    # to store the maximum values
    # of each row and column
    for i in range(M):
        for j in range(N):
 
            # Store the row-wise
            # maximum values
            maxRowVal[i] = max(maxRowVal[i],
                               matrix[i][j])
 
            # Store the column-wise
            # maximum values
            maxColVal[j] = max(maxColVal[j],
                               matrix[i][j])
 
    # Calculate the total amount
    # of increment
    totalIncrease = 0
 
    # Traverse matrix mat[][]
    for i in range(M):
        for j in range(N):
 
            # The maximum possible
            # amount to increase
            needToIncrease = (min(maxRowVal[i],
                                  maxColVal[j]) -
                                  matrix[i][j])
 
            # Total increased value
            totalIncrease += needToIncrease
 
    # Return the total
    # increased value
    return totalIncrease
 
# Driver Code
if __name__ == '__main__':
     
    # Given matrix
    matrix = [ [ 3, 0, 8, 4 ],
               [ 2, 4, 5, 7 ],
               [ 9, 2, 6, 3 ],
               [ 0, 3, 1, 0 ] ]
 
    # Function call
    print(maxIncrease(matrix))
 
# This code is contributed mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum increment
// in each cell of the given matrix such
// that maximum and minimum value remains
// unaffected
static int maxIncrease(int [,] matrix)
{
     
    // Get the row of matrix as M
    int M = matrix.GetLength(0);
 
    // Get the column of matrix as N
    int N = matrix.GetLength(1);
 
    // To store the row-wise
    // maximum values
    int []maxRowVal = new int[M];
 
    // To store the column-wise
    // maximum values
    int []maxColVal = new int[N];
     
    // Traverse along the matrix
    // to store the maximum values
    // of each row and column
    for(int i = 0; i < M; i++)
    {
       for(int j = 0; j < N; j++)
       {
           
          // Store the row-wise
          // maximum values
          maxRowVal[i] = Math.Max(maxRowVal[i],
                                  matrix[i, j]);
           
          // Store the column-wise
          // maximum values
          maxColVal[j] = Math.Max(maxColVal[j],
                                  matrix[i, j]);
       }
    }
 
    // Calculate the total amount
    // of increment
    int totalIncrease = 0;
 
    // Traverse matrix [,]mat
    for(int i = 0; i < M; i++)
    {
       for(int j = 0; j < N; j++)
       {
            
          // The maximum possible
          // amount to increase
          int needToIncrease = Math.Min(maxRowVal[i],
                                        maxColVal[j]) -
                                        matrix[i, j];
           
          // Total increased value
          totalIncrease += needToIncrease;
       }
    }
     
    // Return the total
    // increased value
    return totalIncrease;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given matrix
    int [,] matrix = { { 3, 0, 8, 4 },
                       { 2, 4, 5, 7 },
                       { 9, 2, 6, 3 },
                       { 0, 3, 1, 0 } };
 
    // Function call
    Console.Write(maxIncrease(matrix) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
35

时间复杂度: O(M * N)