📜  平均至少为 K 的平方子矩阵的计数

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

平均至少为 K 的平方子矩阵的计数

给定一个大小为NxM的矩阵arr[][]和一个整数K ,任务是找到给定矩阵中元素的平均值大于或等于K的平方子矩阵的计数。

例子:

朴素方法:最简单的方法是生成所有可能的平方子矩阵,并检查子平方的所有元素的总和是否大于或等于K乘以子矩阵的大小。

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

高效方法:上述方法可以使用前缀和矩阵进行优化,这导致子矩阵之和的恒定时间计算。请按照以下步骤解决问题:

  • 初始化一个变量,比如count0以存储平均值大于或等于K的子矩阵的计数。
  • 计算矩阵arr[][]的前缀和并将其存储在一个向量中,比如pre[][]
  • 使用变量ij遍历矩阵的每个元素并执行以下步骤:
    • 初始化两个变量,比如lirj
    • 迭代直到lr大于0 ,并且在每次迭代中执行以下步骤:
      • 计算右下顶点为(i, j)和左上顶点为(l, r)的平方子矩阵之和,并将其存储在变量中,例如sum ,即sum = pre[i][j] – pre[l-1][r] – pre[l][r-1] + pre[l-1][r-1]
        • 现在,如果K*(i-l+1)*(j-r+1)的值等于sum ,则将count增加1
        • lr1
  • 最后,完成以上步骤后,打印count的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
#define MAX 1000
 
// Function to count submatrixes with
// average greater than or equals to K
int cntMatrices(vector > arr, int N, int M,
                int K)
{
 
    // Stores count of submatrices
    int cnt = 0;
 
    // Stores the prefix sum of matrix
    vector > pre(N + 1, vector(M + 1, 0));
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Iterate over the range
        // [1, M]
        for (int j = 1; j <= M; j++) {
 
            // Update the prefix sum
            pre[i][j] = arr[i - 1][j - 1] + pre[i - 1][j]
                        + pre[i][j - 1] - pre[i - 1][j - 1];
        }
    }
 
    // Iterate over the range [1, N]
    for (int i = 1; i <= N; i++) {
 
        // Iterate over the range
        // [1, M]
        for (int j = 1; j <= M; j++) {
 
            // Iterate until l and r
            // are greater than 0
            for (int l = i, r = j; l > 0 && r > 0;
                 l--, r--) {
 
                // Update count
                int sum1 = (K * (i - l + 1) * (i - r + 1));
 
                // Stores sum of submatrix
                // with bottom right corner
                // as (i, j) and top left
                // corner as (l, r)
                int sum2 = pre[i][j] - pre[l - 1][r]
                           - pre[l][r - 1]
                           + pre[l - 1][r - 1];
 
                // If sum1 is less than or
                // equal to sum2
                if (sum1 <= sum2)
 
                    // Increment cnt by 1
                    cnt++;
            }
        }
    }
 
    // Return cnt as the answer
    return cnt;
}
 
// Driver Code
int main()
{
    // Given Input
    vector > arr
        = { { 2, 2, 3 }, { 3, 4, 5 }, { 4, 5, 5 } };
    int K = 4;
    int N = arr.size();
    int M = arr[0].size();
 
    // Function Call
    cout << cntMatrices(arr, N, M, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
static int MAX = 1000;
 
// Function to count submatrixes with
// average greater than or equals to K
static int cntMatrices(int[][] arr, int N,
                    int M, int K)
{
     
    // Stores count of submatrices
    int cnt = 0;
 
    // Stores the prefix sum of matrix
    int[][] pre = new int[N + 1][M + 1];
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Iterate over the range
        // [1, M]
        for(int j = 1; j <= M; j++)
        {
             
            // Update the prefix sum
            pre[i][j] = arr[i - 1][j - 1] + pre[i - 1][j] +
                            pre[i][j - 1] - pre[i - 1][j - 1];
        }
    }
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
         
        // Iterate over the range
        // [1, M]
        for(int j = 1; j <= M; j++)
        {
             
            // Iterate until l and r
            // are greater than 0
            for(int l = i, r = j;
                    l > 0 && r > 0; l--, r--)
            {
                 
                // Update count
                int sum1 = (K * (i - l + 1) *
                                (i - r + 1));
 
                // Stores sum of submatrix
                // with bottom right corner
                // as (i, j) and top left
                // corner as (l, r)
                int sum2 = pre[i][j] - pre[l - 1][r] -
                       pre[l][r - 1] + pre[l - 1][r - 1];
 
                // If sum1 is less than or
                // equal to sum2
                if (sum1 <= sum2)
 
                    // Increment cnt by 1
                    cnt++;
            }
        }
    }
 
    // Return cnt as the answer
    return cnt;
}
 
// Driver Code
public static void main(String args[])
{
     
    // Given Input
    int[][] arr = { { 2, 2, 3 },
                    { 3, 4, 5 },
                    { 4, 5, 5 } };
    int K = 4;
    int N = arr.length;
    int M = arr[0].length;
 
    // Function Call
    System.out.println( cntMatrices(arr, N, M, K));
}
}
 
// This code is contributed by avijitmondal1998


Python3
# Python3 program for the above approach
# define MAX 1000
 
# Function to count submatrixes with
# average greater than or equals to K
def cntMatrices(arr, N, M, K):
 
    # Stores count of submatrices
    cnt = 0
 
    # Stores the prefix sum of matrix
    pre = [[0 for i in range(M + 1)]
              for i in range(N + 1)]
               
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
         
        # Iterate over the range
        # [1, M]
        for j in range(1, M + 1):
             
            # Update the prefix sum
            pre[i][j] = (arr[i - 1][j - 1] +
                         pre[i - 1][j] +
                         pre[i][j - 1] -
                         pre[i - 1][j - 1])
 
    # Iterate over the range [1, N]
    for i in range(1, N + 1):
         
        # Iterate over the range
        # [1, M]
        for j in range(1, M + 1):
             
            # Iterate until l and r
            # are greater than 0
            l, r = i, j
            while l > 0 and r > 0:
                 
                # Update count
                sum1 = (K * (i - l + 1) * (i - r + 1))
 
                # Stores sum of submatrix
                # with bottom right corner
                # as (i, j) and top left
                # corner as (l, r)
                sum2 = (pre[i][j] -
                        pre[l - 1][r] - pre[l][r - 1] +
                        pre[l - 1][r - 1])
 
                # If sum1 is less than or
                # equal to sum2
                if (sum1 <= sum2):
                     
                    # Increment cnt by 1
                    cnt += 1
                     
                l -= 1
                r -= 1
 
    # Return cnt as the answer
    return cnt
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    arr = [ [ 2, 2, 3 ],
            [ 3, 4, 5 ],
            [ 4, 5, 5 ] ]
    K = 4
    N = len(arr)
    M = len(arr[0])
 
    # Function Call
    print(cntMatrices(arr, N, M, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
class GFG
{
 
  static int MAX = 1000;
 
  // Function to count submatrixes with
  // average greater than or equals to K
  static int cntMatrices(int[,] arr, int N,
                         int M, int K)
  {
 
    // Stores count of submatrices
    int cnt = 0;
 
    // Stores the prefix sum of matrix
    int[,] pre = new int[N + 1, M + 1];
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
 
      // Iterate over the range
      // [1, M]
      for(int j = 1; j <= M; j++)
      {
 
        // Update the prefix sum
        pre[i, j] = arr[i - 1, j - 1] + pre[i - 1, j] +
          pre[i, j - 1] - pre[i - 1, j - 1];
      }
    }
 
    // Iterate over the range [1, N]
    for(int i = 1; i <= N; i++)
    {
 
      // Iterate over the range
      // [1, M]
      for(int j = 1; j <= M; j++)
      {
 
        // Iterate until l and r
        // are greater than 0
        for(int l = i, r = j;
            l > 0 && r > 0; l--, r--)
        {
 
          // Update count
          int sum1 = (K * (i - l + 1) *
                      (i - r + 1));
 
          // Stores sum of submatrix
          // with bottom right corner
          // as (i, j) and top left
          // corner as (l, r)
          int sum2 = pre[i, j] - pre[l - 1, r] -
            pre[l, r - 1] + pre[l - 1, r - 1];
 
          // If sum1 is less than or
          // equal to sum2
          if (sum1 <= sum2)
 
            // Increment cnt by 1
            cnt++;
        }
      }
    }
 
    // Return cnt as the answer
    return cnt;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
 
    // Given Input
    int[,] arr = { { 2, 2, 3 },
                  { 3, 4, 5 },
                  { 4, 5, 5 } };
    int K = 4;
    int N = arr.GetLength(0);
    int M = arr.GetLength(0);
 
    // Function Call
    Console.WriteLine( cntMatrices(arr, N, M, K));
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
7

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