📜  计算给定矩阵的所有单元格之和等于S的方子矩阵的数量| 2套

📅  最后修改于: 2022-05-13 01:57:23.129000             🧑  作者: Mango

计算给定矩阵的所有单元格之和等于S的方子矩阵的数量| 2套

给定一个矩阵,维度为M*N的矩阵 arr[][]和一个整数S ,任务是打印矩阵的子平方数的计数,其总和等于S

例子:

朴素方法:解决问题的最简单方法是生成所有可能的子平方,并检查子平方的所有元素的总和等于S

时间复杂度: O(N 3 * M 3 )
辅助空间: O(1)

替代方法:上述方法可以通过找到矩阵的前缀和来优化,这导致子矩阵所有元素的总和的恒定时间计算。请按照以下步骤解决问题:

  • 找到矩阵arr[][]的前缀和并将其存储在一个二维向量中,比如维度为(M + 1)*(N + 1) 的dp。
  • 初始化一个变量,比如count0 ,以存储总和为S 的子平方数。
  • 使用变量x在范围[1, min(N, M)] 上迭代并执行以下操作:
    • 使用变量ij迭代矩阵arr[][] 的每个元素并执行以下操作:
      • 如果ij大于或等于x,则找到维度 x * x的子平方和,其中(i, j)作为变量中的右下顶点,例如Sum。
      • Sum变量更新为 , Sum = dp[i][j] – dp[i – x][j] – dp[i][j – x] + dp[i – x][j – x]。
      • 如果Sum等于S,则将计数加 1。
  • 最后,完成上述步骤后,将count 中的值打印为 答案

下面是上述方法的实现:



C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to compute prefix sum
void find_prefixsum(int M, int N, vector >& arr,
                    vector >& dp)
{
    // Assign 0 to first column
    for (int i = 0; i <= M; i++) {
        dp[i][0] = 0;
    }
 
    // Assign 0 to first row
    for (int j = 0; j <= N; j++) {
        dp[0][j] = 0;
    }
    // Iterate over the range
    //[1, M]
    for (int i = 1; i <= M; i++) {
 
        // Iterate over the range
        //[1, N]
        for (int j = 1; j <= N; j++) {
 
            // Update
            dp[i][j] = dp[i][j - 1] + arr[i - 1][j - 1];
        }
    }
 
    // Iterate over the range
    //[1, M]
    for (int i = 1; i <= M; i++) {
 
        // Iterate over the range
        //[1, N]
        for (int j = 1; j <= N; j++) {
 
            // Update
            dp[i][j] = dp[i][j] + dp[i - 1][j];
        }
    }
}
 
// Function to find sub-squares
// with given sum S
int findSubsquares(vector > arr, int M, int N,
                   int S)
{
    // Stores the prefix sum of
    // matrix
    vector > dp(M + 1, vector(N + 1));
 
    // Function call to find
    // prefix Sum of matrix
    find_prefixsum(M, N, arr, dp);
 
    // Stores the count of
    // subsquares with sum S
    int cnt = 0;
 
    for (int x = 1; x <= min(N, M); x++) {
 
        // Iterate over every
        // element of the matrix
        for (int i = x; i <= M; i++) {
            for (int j = x; j <= N; j++) {
 
                // Stores the sum of
                // subsquare of dimension
                // x*x formed with i, j as
                // the bottom-right
                // vertex
 
                int sum = dp[i][j] - dp[i - x][j]
                          - dp[i][j - x] + dp[i - x][j - x];
 
                // If sum is equal to S
 
                if (sum == S) {
 
                    // Increment cnt by 1
                    cnt++;
                }
            }
        }
    }
 
    // Return the result
    return cnt;
}
 
// Driver Code
int main()
{
    // Input
    int M = 4, N = 5;
    vector > arr = { { 2, 4, 3, 2, 10 },
                                 { 3, 1, 1, 1, 5 },
                                 { 1, 1, 2, 1, 4 },
                                 { 2, 1, 1, 1, 3 } };
    int S = 10;
 
    // Function call
    cout << findSubsquares(arr, M, N, S);
    return 0;
}


Python3
# python program for the above approach
 
# Function to compute prefix sum
def find_prefixsum(M, N, arr, dp):
   
    # Assign 0 to first column
    for i in range(0, M+1):
        dp[i][0] = 0
 
    # Assign 0 to first row
    for j in range(0, N+1):
        dp[0][j] = 0
         
    # Iterate over the range
    # [1, M]
    for i in range(1, M+1):
       
        # Iterate over the range
        # [1, N]
        for j in range(1, N+1):
            dp[i][j] = dp[i][j - 1] + arr[i - 1][j - 1]
 
    # Iterate over the range
    # [1, M]
    for i in range(1, M+1):
 
        # Iterate over the range
        #  [1, N]
        for j in range(1, N+1):
            # Update
            dp[i][j] = dp[i][j] + dp[i - 1][j]
 
# Function to find sub-squares
# with given sum S
def findSubsquares(arr, M,  N, S):
   
    # Stores the prefix sum of
    # matrix
    dp = [[0 for i in range(N + 1)] for col in range(M + 1)]
     
    # Function call to find
    # prefix Sum of matrix
    find_prefixsum(M, N, arr, dp)
     
    # Stores the count of
    # subsquares with sum S
    cnt = 0
    for x in range(1, min(N, M)):
 
        # Iterate over every
        # element of the matrix
        for i in range(x, M + 1):
            for j in range(x, N + 1):
 
                # Stores the sum of
                # subsquare of dimension
                # x*x formed with i, j as
                # the bottom-right
                # vertex
                sum = dp[i][j] - dp[i - x][j] - dp[i][j - x] + dp[i - x][j - x]
                 
                # If sum is equal to S
 
                if (sum == S):
                    # Increment cnt by 1
                    cnt += 1
 
    # Return the result
    return cnt
 
# Driver Code
# Input
M = 4
N = 5
arr = [[2, 4, 3, 2, 10],
       [3, 1, 1, 1, 5],
       [1, 1, 2, 1, 4],
       [2, 1, 1, 1, 3]]
S = 10
 
# Function call
print(findSubsquares(arr, M, N, S))
 
# This code is contributed by amreshkumar3


Javascript



输出
3

时间复杂度: O(M * N * min(M, N))
辅助空间: O(N * M)

高效的方法:可以使用二分搜索算法进一步优化上述方法。请参阅有效解决方案的链接 [Count of submatrix with sum X in a given Matrix]。

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。