📌  相关文章
📜  通过旋转所有行或所有列来最大化矩阵的对角线和

📅  最后修改于: 2021-09-07 05:27:01             🧑  作者: Mango

给定一个维度为N * N 的方阵mat[][] ,任务是通过将矩阵的所有行或所有列旋转一个正整数来找到给定矩阵中可能的对角元素的最大和。

例子:

方法:思想是用所有可能的方式旋转矩阵的所有行和列,并计算得到的最大和。请按照以下步骤解决问题:

  • 初始化一个变量,比如maxDiagonalSum ,通过旋转矩阵的所有行或列来存储矩阵对角元素的最大可能总和。
  • 将矩阵的所有行旋转[0, N – 1]范围内的一个正整数,并更新maxDiagonalSum的值。
  • 将矩阵的所有列旋转[0, N – 1]范围内的一个正整数,并更新maxDiagonalSum的值。
  • 最后,打印maxDiagonalSum的值。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
#define N 3
 
 
// Function to find maximum sum of diagonal elements
// of matrix by rotating either rows or columns
int findMaximumDiagonalSumOMatrixf(int A[][N])
{
     
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = INT_MIN;
     
 
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
         
 
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
 
        // Calculate sum of diagonal
        // elements of the matrix
        for (int j = 0; j < N; j++) {
             
 
            // Update curr
            curr += A[j][(i + j) % N];
        }
         
         
        // Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                                      curr);
    }
 
 
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for (int i = 0; i < N; i++) {
         
 
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
 
        // Calculate sum of diagonal
        // elements of the matrix
        for (int j = 0; j < N; j++) {
             
 
            // Update curr
            curr += A[(i + j) % N][j];
        }
         
         
        // Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                                      curr);
    }
 
       
    return maxDiagonalSum;
}
 
 
// Driver code
int main()
{
     
    int mat[N][N] = { { 1, 1, 2 },
                    { 2, 1, 2 },
                    { 1, 2, 2 } };
     
    cout<< findMaximumDiagonalSumOMatrixf(mat);
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
static int N = 3;
  
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int A[][])
{
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = Integer.MIN_VALUE;
     
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[j][(i + j) % N];
        }
          
        // Update maxDiagonalSum
        maxDiagonalSum = Math.max(maxDiagonalSum,
                                  curr);
    }
     
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
         
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[(i + j) % N][j];
        }
         
        // Update maxDiagonalSum
        maxDiagonalSum = Math.max(maxDiagonalSum,
                                  curr);
    }
    return maxDiagonalSum;
}
  
// Driver Code
public static void main(String[] args)
{
    int[][] mat = { { 1, 1, 2 },
                    { 2, 1, 2 },
                    { 1, 2, 2 } };
      
    System.out.println(
        findMaximumDiagonalSumOMatrixf(mat));
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program to implement
# the above approach
import sys
 
N = 3
 
# Function to find maximum sum of diagonal
# elements of matrix by rotating either
# rows or columns
def findMaximumDiagonalSumOMatrixf(A):
     
    # Stores maximum diagonal sum of elements
    # of matrix by rotating rows or columns
    maxDiagonalSum = -sys.maxsize - 1
 
    # Rotate all the columns by an integer
    # in the range [0, N - 1]
    for i in range(N):     
 
        # Stores sum of diagonal elements
        # of the matrix
        curr = 0
         
        # Calculate sum of diagonal
        # elements of the matrix
        for j in range(N):
             
            # Update curr
            curr += A[j][(i + j) % N]
        
        # Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                             curr)
                              
    # Rotate all the rows by an integer
    # in the range [0, N - 1]
    for i in range(N):
         
        # Stores sum of diagonal elements
        # of the matrix
        curr = 0
         
        # Calculate sum of diagonal
        # elements of the matrix
        for j in range(N):         
             
            # Update curr
            curr += A[(i + j) % N][j]
         
        # Update maxDiagonalSum
        maxDiagonalSum = max(maxDiagonalSum,
                             curr)
                              
    return maxDiagonalSum
 
# Driver code
if __name__ == "__main__":
     
    mat = [ [ 1, 1, 2 ],
            [ 2, 1, 2 ],
            [ 1, 2, 2 ] ]
     
    print(findMaximumDiagonalSumOMatrixf(mat))
     
# This code is contributed by chitranayal


C#
// C# program to implement
// the above approach 
using System;
   
class GFG{
   
static int N = 3;
   
// Function to find maximum sum of
// diagonal elements of matrix by
// rotating either rows or columns
static int findMaximumDiagonalSumOMatrixf(int[,] A)
{
     
    // Stores maximum diagonal sum of elements
    // of matrix by rotating rows or columns
    int maxDiagonalSum = Int32.MinValue;
     
    // Rotate all the columns by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
          
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[j, (i + j) % N];
        }
         
        // Update maxDiagonalSum
        maxDiagonalSum = Math.Max(maxDiagonalSum,
                                  curr);
    }
      
    // Rotate all the rows by an integer
    // in the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Stores sum of diagonal elements
        // of the matrix
        int curr = 0;
          
        // Calculate sum of diagonal
        // elements of the matrix
        for(int j = 0; j < N; j++)
        {
             
            // Update curr
            curr += A[(i + j) % N, j];
        }
          
        // Update maxDiagonalSum
        maxDiagonalSum = Math.Max(maxDiagonalSum,
                                  curr);
    }
    return maxDiagonalSum;
}
   
// Driver Code
public static void Main()
{
    int[,] mat = { { 1, 1, 2 },
                   { 2, 1, 2 },
                   { 1, 2, 2 } };
       
    Console.Write(findMaximumDiagonalSumOMatrixf(mat));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
6

时间复杂度: O(N 2 )
辅助空间: O(1)

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