📜  查找元素总和为K的正方形子矩阵的最大长度

📅  最后修改于: 2021-06-26 20:34:52             🧑  作者: Mango

给定一个N x M矩阵,其中N是给定矩阵中的行数, M是列数和整数K。任务是找到元素总和小于或等于K的正方形子矩阵的最大长度,如果没有此类正方形,则打印0

例子:

Input: r = 4, c = 4 , k = 6
matrix[][] = { {1, 1, 1, 1},
               {1, 0, 0, 0},
               {1, 0, 0, 0},
               {1, 0, 0, 0},
             } 
Output: 3
Explanation:
Square from (0,0) to (2,2) with 
sum 5 is one of the valid answer.

Input: r = 4, c = 4 , k = 1
matrix[][] = { {2, 2, 2},
               {2, 2, 2},
               {2, 2, 2},
               {2, 2, 2},
             } 
Output: 0
Explanation:
There is no valid answer.

方法:
这个想法是计算前缀和矩阵。一旦计算了前缀和矩阵,就可以以O(1)时间复杂度来计算所需的子矩阵和。使用滑动窗口技术来计算最大长度的正方形子矩阵。对于每个长度为cur_max + 1的正方形,其中cur_max是当前找到的正方形子矩阵的最大长度,我们检查当前子矩阵中具有cur_max + 1长度的元素之和是否小于或等于K 。如果是,则将结果加1,否则我们将继续进行检查。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
    // Function to return maximum 
    // length of square submatrix
    // having sum of elements at-most K
    int maxLengthSquare(int row, int column, 
                        int arr[][4], int k)
    {
        // Matrix to store prefix sum
        int sum[row + 1][column + 1] ;
      
        for(int i = 1; i <= row; i++)
            for(int j = 0; j <= column; j++)
                sum[i][j] = 0;
                  
        // Current maximum length
        int cur_max = 1;
      
        // Variable for storing 
        // maximum length of square
        int max = 0;
              
        for (int i = 1; i <= row; i++) 
        {
            for (int j = 1; j <= column; j++) 
            {
                // Calculating prefix sum
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + 
                            arr[i - 1][j - 1] - sum[i - 1][j - 1];
          
                // Checking whether there 
                // exits square with length 
                // cur_max+1 or not
                if(i >= cur_max && j >= cur_max && 
                    sum[i][j] - sum[i - cur_max][j]
                    - sum[i][j - cur_max] + 
                    sum[i - cur_max][j - cur_max] <= k)
                {
                    max = cur_max++;
                }
            }
        }
      
        // Returning the 
        // maximum length
        return max;
    }
  
    // Driver code
    int main() 
    {
          
        int row = 4, column = 4;
        int matrix[4][4] = { {1, 1, 1, 1},
                        {1, 0, 0, 0},
                        {1, 0, 0, 0},
                        {1, 0, 0, 0}
                        };
      
        int k = 6; 
        int ans = maxLengthSquare(row, column, matrix, k);
        cout << ans;
          
        return 0;
    }
  
// This code is contributed by AnkitRai01


Java
// Java implementation of 
// the above approach
import java.util.*;
  
class GFG
{
    // Function to return maximum 
    // length of square submatrix
    // having sum of elements at-most K
    public static int maxLengthSquare(int row,int column,
                                        int[][] arr, int k)
    {
        // Matrix to store prefix sum
        int sum[][] = new int[row + 1][column + 1];
      
        // Current maximum length
        int cur_max = 1;
      
        // Variable for storing 
        // maximum length of square
        int max = 0;
              
        for (int i = 1; i <= row; i++) 
        {
            for (int j = 1; j <= column; j++) 
            {
                // Calculating prefix sum
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + 
                            arr[i - 1][j - 1] - sum[i - 1][j - 1];
          
                // Checking whether there 
                // exits square with length 
                // cur_max+1 or not
                if(i >=cur_max&&j>=cur_max&&sum[i][j]-sum[i - cur_max][j]
                            - sum[i][j - cur_max] 
                            + sum[i - cur_max][j - cur_max] <= k){
                    max = cur_max++;
                }
            }
        }
      
        // Returning the 
        // maximum length
        return max;
    }
  
    // Driver code
    public static void main(String args[]) 
    {
          
        int row = 4 , column = 4;
        int matrix[][] = { {1, 1, 1, 1},
                        {1, 0, 0, 0},
                        {1, 0, 0, 0},
                        {1, 0, 0, 0}
                        };
      
        int k = 6; 
        int ans = maxLengthSquare(row,column,matrix, k);
        System.out.println(ans);
    }
}


Python3
# Python3 implementation of the above approach 
import numpy as np
  
# Function to return maximum 
# length of square submatrix 
# having sum of elements at-most K 
def maxLengthSquare(row, column, arr, k) :
      
    # Matrix to store prefix sum 
    sum = np.zeros((row + 1, column + 1)); 
      
    # Current maximum length 
    cur_max = 1; 
      
    # Variable for storing 
    # maximum length of square 
    max = 0; 
              
    for i in range(1, row + 1) :
        for j in range(1, column + 1) :
              
            # Calculating prefix sum
            sum[i][j] = sum[i - 1][j] + sum[i][j - 1] + \
                        arr[i - 1][j - 1] - \
                        sum[i - 1][j - 1];
              
            # Checking whether there
            # exits square with length
            # cur_max+1 or not
            if(i >= cur_max and j >= cur_max and 
                 sum[i][j] - sum[i - cur_max][j] - sum[i][j - 
                                     cur_max] + sum[i - 
                                     cur_max][j - cur_max] <= k) :
                max = cur_max; 
                cur_max += 1;
      
    # Returning the maximum length 
    return max; 
      
# Driver code 
if __name__ == "__main__" :
      
    row = 4 ;
    column = 4;
    matrix = [[1, 1, 1, 1],
              [1, 0, 0, 0],
              [1, 0, 0, 0],
              [1, 0, 0, 0]];
    k = 6;
    ans = maxLengthSquare(row, column, matrix, k);
    print(ans); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the above approach 
using System;
  
class GFG 
{ 
    // Function to return maximum 
    // length of square submatrix 
    // having sum of elements at-most K 
    public static int maxLengthSquare(int row,int column, 
                                        int[,] arr, int k) 
    { 
        // Matrix to store prefix sum 
        int [,]sum = new int[row + 1,column + 1]; 
      
        // Current maximum length 
        int cur_max = 1; 
      
        // Variable for storing 
        // maximum length of square 
        int max = 0; 
              
        for (int i = 1; i <= row; i++) 
        { 
            for (int j = 1; j <= column; j++) 
            { 
                // Calculating prefix sum 
                sum[i, j] = sum[i - 1, j] + sum[i, j - 1] + 
                            arr[i - 1, j - 1] - sum[i - 1, j - 1]; 
          
                // Checking whether there 
                // exits square with length 
                // cur_max+1 or not 
                if(i >=cur_max && j>=cur_max && sum[i, j]-sum[i - cur_max, j] 
                            - sum[i, j - cur_max] 
                            + sum[i - cur_max, j - cur_max] <= k)
                { 
                    max = cur_max++; 
                } 
            } 
        } 
      
        // Returning the 
        // maximum length 
        return max; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
          
        int row = 4 , column = 4; 
        int [,]matrix = { {1, 1, 1, 1}, 
                        {1, 0, 0, 0}, 
                        {1, 0, 0, 0}, 
                        {1, 0, 0, 0} 
                        }; 
      
        int k = 6; 
        int ans = maxLengthSquare(row, column, matrix, k); 
        Console.WriteLine(ans); 
    } 
} 
  
// This code is contributed by AnkitRai01


输出:
3

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

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