📜  给定矩阵的所有子矩阵的总和

📅  最后修改于: 2021-06-25 14:21:51             🧑  作者: Mango

给定一个NxN二维矩阵,该任务将查找所有子矩阵的总和。
例子:

Input :  arr[] = {{1, 1},
                  {1, 1}};
Output : 16
Explanation: 
Number of sub-matrices with 1 elements = 4
Number of sub-matrices with 2 elements = 4
Number of sub-matrices with 3 elements = 0
Number of sub-matrices with 4 elements = 1

Since all the entries are 1, the sum becomes
sum = 1 * 4 + 2 * 4 + 3 * 0 + 4 * 1 = 16

Input : arr[] = {{1, 2, 3},
                 {4, 5, 6},
                 {7, 8, 9}}
Output : 500

简单的解决方案:一个简单的解决方案是生成所有可能的子矩阵,并对所有子矩阵求和。
该方法的时间复杂度将为O(n 6 )。

高效的解决方案:对于矩阵的每个元素,让我们尝试查找元素所在的子矩阵的数量。
这可以在O(1)时间内完成。让我们假设元素的索引在基于0的索引中为(X,Y),则该元素的子矩阵(S x,y )的个数可以由公式S x,y =(X + 1)*(Y + 1)*(N-X)*(N-Y) 。该公式有效,因为我们只需要在矩阵上选择两个不同的位置,这将创建一个包围该元素的子矩阵。因此,对于每个元素,“ sum”可以更新为sum + =(S x,y )* Arr x,y

下面是上述方法的实现:

在这里,我们需要尝试在反向查找技术中解决此问题:

1)对于特定元素,将包含此元素可能子矩阵是什么。

2)当我们得到可能的子矩阵的数量时,我们可以通过计算a [i] =当前元素来计算该特定元素的贡献(a [i] *将包含子矩阵的总数)

3)现在问题来了,如何找到特定元素的可能子矩阵的数量。

[[1 2 3]

[4 5 6]

[7 8 9]

因此,让我们考虑当前元素为5,因此对于5有(X + 1)*(Y + 1)个选择,子矩阵起点的坐标可以位于((左上))

同样,将存在(NX)*(NY)个选项,该子矩阵的末端坐标可以位于该位置(右下)

左上角的选择数量=(X + 1)*(Y + 1)

右下角的选择数量=(NX)*(NY)

要包含在子矩阵中的当前元素的选择总数为:(X + 1)*(Y + 1)*(NX)*(NY)

可以包含在所有可能的子矩阵中的当前元素的对接将是= arr [X] [Y] *(X + 1)*(Y + 1)*(NX)*(NY)

其中X和Y是子矩阵的索引。

时间复杂度:O(N ^ 2)

空间复杂度:O(1)

C++
// C++ program to find the sum of all
// possible submatrices of a given Matrix
 
#include 
#define n 3
using namespace std;
 
// Function to find the sum of all
// possible submatrices of a given Matrix
int matrixSum(int arr[][n])
{
    // Variable to store
    // the required sum
    int sum = 0;
 
    // Nested loop to find the number
    // of submatrices, each number belongs to
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
 
            // Number of ways to choose
            // from top-left elements
            int top_left = (i + 1) * (j + 1);
 
            // Number of ways to choose
            // from bottom-right elements
            int bottom_right = (n - i) * (n - j);
            sum += (top_left * bottom_right * arr[i][j]);
        }
 
    return sum;
}
 
// Driver Code
int main()
{
    int arr[][n] = { { 1, 1, 1 },
                     { 1, 1, 1 },
                     { 1, 1, 1 } };
 
    cout << matrixSum(arr);
 
    return 0;
}


Java
// Java program to find the sum of all
// possible submatrices of a given Matrix
class GFG
{
 
    static final int n = 3;
 
    // Function to find the sum of all
    // possible submatrices of a given Matrix
    static int matrixSum(int arr[][])
    {
        // Varialbe to store
        // the required sum
        int sum = 0;
 
        // Nested loop to find the number
        // of submatrices, each number belongs to
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
 
                // Number of ways to choose
                // from top-left elements
                int top_left = (i + 1) * (j + 1);
 
                // Number of ways to choose
                // from bottom-right elements
                int bottom_right = (n - i) * (n - j);
                sum += (top_left * bottom_right * arr[i][j]);
            }
        }
 
        return sum;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[][] = {{1, 1, 1},
        {1, 1, 1},
        {1, 1, 1}};
 
        System.out.println(matrixSum(arr));
    }
}
 
// This code contributed by Rajput-Ji


Python3
# Python3 program to find the sum of all
# possible submatrices of a given Matrix
n = 3
 
# Function to find the sum of all
# possible submatrices of a given Matrix
def matrixSum(arr) :
     
    # Variable to store the required sum
    sum = 0;
 
    # Nested loop to find the number of
    # submatrices, each number belongs to
    for i in range(n) :
        for j in range(n) :
 
            # Number of ways to choose
            # from top-left elements
            top_left = (i + 1) * (j + 1);
 
            # Number of ways to choose
            # from bottom-right elements
            bottom_right = (n - i) * (n - j);
            sum += (top_left * bottom_right *
                                  arr[i][j]);
 
    return sum;
 
# Driver Code
if __name__ == "__main__" :
    arr = [[ 1, 1, 1 ],
           [ 1, 1, 1 ],
           [ 1, 1, 1 ]];
 
    print(matrixSum(arr))
     
# This code is contributed by Ryuga


C#
// C# program to find the sum of all
// possible submatrices of a given Matrix
using System;
 
class GFG
{
static int n = 3;
 
// Function to find the sum of all
// possible submatrices of a given Matrix
static int matrixSum(int [,]arr)
{
    // Varialbe to store the
    // required sum
    int sum = 0;
 
    // Nested loop to find the number of 
    // submatrices, each number belongs to
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
 
            // Number of ways to choose
            // from top-left elements
            int top_left = (i + 1) * (j + 1);
 
            // Number of ways to choose
            // from bottom-right elements
            int bottom_right = (n - i) * (n - j);
            sum += (top_left * bottom_right * arr[i, j]);
        }
    }
 
    return sum;
}
 
// Driver Code
public static void Main()
{
    int [,]arr = {{1, 1, 1},
    {1, 1, 1},
    {1, 1, 1}};
 
    Console.WriteLine(matrixSum(arr));
}
}
 
// This code contributed by vt_m..


PHP


Javascript


输出:
100

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。