📜  寻找所有元素相等的最大平方子矩阵

📅  最后修改于: 2021-09-17 16:09:05             🧑  作者: Mango

给定一个 N x N 矩阵,确定最大 K,使得 K x K 是一个具有所有相等元素的子矩阵,即该子矩阵中的所有元素必须相同。
约束:
1 <= N <= 1000
0 <= A i , j <= 10 9
例子:

Input : a[][] = {{2, 3, 3},
                 {2, 3, 3},
                 {2, 2, 2}}
Output : 2
Explanation: A 2x2 matrix is formed from index
A0,1 to A1,2

Input : a[][]  = {{9, 9, 9, 8},
                  {9, 9, 9, 6},
                  {9, 9, 9, 3},
                  {2, 2, 2, 2}
Output : 3
Explanation : A 3x3 matrix is formed from index
A0,0 to A2,2

方法一(天真的方法)
我们可以很容易地找到为O的所有子矩阵正方形(N 3)和时间检查每个子矩阵是否包含相等的元件或不为O(n 2)时间这使得运行算法为O(N 5)的总时间。
方法二(动态规划)
对于每个单元格 (i, j),我们存储 K 的最大值,使得 K x K 是一个子矩阵,其中所有元素都相等,并且 (i, j) 的位置是最右下角的元素。
DP i,j取决于 {DP i-1, j , DP i, j-1 , DP i-1, j-1 }

If Ai, j is equal to {Ai-1, j, Ai, j-1, Ai-1, j-1}, 
   all the three values:
    DPi, j = min(DPi-1, j, DPi, j-1, DPi-1, j-1) + 1
Else
    DPi, j = 1  // Matrix Size 1 

The answer would be the maximum of all DPi, j's

下面是上述步骤的实现。

C++
// C++ program to find maximum K such that K x K
// is a submatrix with equal elements.
#include
#define Row 6
#define Col 6
using namespace std;
 
// Returns size of the largest square sub-matrix
// with all same elements.
int largestKSubmatrix(int a[][Col])
{
    int dp[Row][Col];
    memset(dp, sizeof(dp), 0);
 
    int result = 0;
    for (int i = 0 ; i < Row ; i++)
    {
        for (int j = 0 ; j < Col ; j++)
        {
            // If elements is at top row or first
            // column, it wont form a  square
            // matrix's bottom-right
            if (i == 0 || j == 0)
                dp[i][j] = 1;
 
            else
            {
                // Check if adjacent elements are equal
                if (a[i][j] == a[i-1][j] &&
                    a[i][j] == a[i][j-1] &&
                    a[i][j] == a[i-1][j-1] )
                    dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]),
                                      dp[i-1][j-1] ) + 1;
 
                // If not equal, then it will form a 1x1
                // submatrix
                else dp[i][j] = 1;
            }
 
            // Update result at each (i,j)
            result = max(result, dp[i][j]);
        }
    }
 
    return result;
}
 
// Driven Program
int main()
{
    int a[Row][Col] = { 2, 2, 3, 3, 4, 4,
                        5, 5, 7, 7, 7, 4,
                        1, 2, 7, 7, 7, 4,
                        4, 4, 7, 7, 7, 4,
                        5, 5, 5, 1, 2, 7,
                        8, 7, 9, 4, 4, 4
                      };
 
    cout << largestKSubmatrix(a) << endl;
 
    return 0;
}


Java
// Java program to find maximum
// K such that K x K is a
// submatrix with equal elements.
class GFG
{
    static int Row = 6, Col = 6;
     
    // Returns size of the largest
    // square sub-matrix with
    // all same elements.
    static int largestKSubmatrix(int [][]a)
    {
        int [][]dp = new int [Row][Col];
        int result = 0;
        for (int i = 0 ;
                 i < Row ; i++)
        {
            for (int j = 0 ;
                     j < Col ; j++)
            {
                // If elements is at top
                // row or first column,
                // it wont form a square
                // matrix's bottom-right
                if (i == 0 || j == 0)
                    dp[i][j] = 1;
 
                else
                {
                    // Check if adjacent
                    // elements are equal
                    if (a[i][j] == a[i - 1][j] &&
                        a[i][j] == a[i][j - 1] &&
                        a[i][j] == a[i - 1][j - 1])
                    {
                    dp[i][j] = (dp[i - 1][j] > dp[i][j - 1] &&
                                dp[i - 1][j] > dp[i - 1][j - 1] + 1) ?
                                                        dp[i - 1][j] :
                               (dp[i][j - 1] > dp[i - 1][j] &&
                                dp[i][j - 1] > dp[i - 1][j - 1] + 1) ?
                                                        dp[i][j - 1] :
                                                 dp[i - 1][j - 1] + 1;
                    }            
 
                    // If not equal, then it
                    // will form a 1x1 submatrix
                    else dp[i][j] = 1;
                }
             
                // Update result at each (i,j)
                result = result > dp[i][j] ?
                                    result : dp[i][j];
            }
        }
        return result;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int [][]a = {{2, 2, 3, 3, 4, 4},
                     {5, 5, 7, 7, 7, 4},
                     {1, 2, 7, 7, 7, 4},
                     {4, 4, 7, 7, 7, 4},
                     {5, 5, 5, 1, 2, 7},
                      {8, 7, 9, 4, 4, 4}};
 
        System.out.println(largestKSubmatrix(a));
 
    }
}
 
// This code is contributed
// by ChitraNayal


C#
// C# program to find maximum
// K such that K x K is a
// submatrix with equal elements.
using System;
 
class GFG
{
    static int Row = 6, Col = 6;
     
    // Returns size of the
    // largest square sub-matrix
    // with all same elements.
    static int largestKSubmatrix(int[,] a)
    {
        int[,] dp = new int [Row, Col];
        int result = 0;
        for (int i = 0 ; i < Row ; i++)
        {
            for (int j = 0 ;
                     j < Col ; j++)
            {
                // If elements is at top
                // row or first column,
                // it wont form a square
                // matrix's bottom-right
                if (i == 0 || j == 0)
                    dp[i, j] = 1;
 
                else
                {
                    // Check if adjacent
                    // elements are equal
                    if (a[i, j] == a[i - 1, j] &&
                        a[i, j] == a[i, j - 1] &&
                        a[i, j] == a[i - 1, j - 1])
                    {
                     dp[i, j] = (dp[i - 1, j] > dp[i, j - 1] &&
                                 dp[i - 1, j] > dp[i - 1, j - 1] + 1) ?
                                                         dp[i - 1, j] :
                                 (dp[i, j - 1] > dp[i - 1, j] &&
                                  dp[i, j - 1] > dp[i - 1, j - 1] + 1) ?
                                                          dp[i, j - 1] :
                                                   dp[i - 1, j - 1] + 1;
                    }            
 
                    // If not equal, then
                    // it will form a 1x1
                    // submatrix
                    else dp[i, j] = 1;
                }
             
                // Update result at each (i,j)
                result = result > dp[i, j] ?
                                    result : dp[i, j];
            }
        }
        return result;
    }
 
    // Driver Code
    public static void Main()
    {
        int[,] a = {{2, 2, 3, 3, 4, 4},
                    {5, 5, 7, 7, 7, 4},
                    {1, 2, 7, 7, 7, 4},
                    {4, 4, 7, 7, 7, 4},
                    {5, 5, 5, 1, 2, 7},
                    {8, 7, 9, 4, 4, 4}};
 
        Console.Write(largestKSubmatrix(a));
    }
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 program to find
# maximum K such that K x K
# is a submatrix with equal
# elements.
Row = 6
Col = 6
 
# Returns size of the
# largest square sub-matrix
# with all same elements.
def largestKSubmatrix(a):
    dp = [[0 for x in range(Row)]
             for y in range(Col)]
 
    result = 0
    for i in range(Row ):
        for j in range(Col):
             
            # If elements is at top
            # row or first column,
            # it wont form a square
            # matrix's bottom-right
            if (i == 0 or j == 0):
                dp[i][j] = 1
 
            else:
                 
                # Check if adjacent
                # elements are equal
                if (a[i][j] == a[i - 1][j] and
                    a[i][j] == a[i][j - 1] and
                    a[i][j] == a[i - 1][j - 1]):
                     
                    dp[i][j] = min(min(dp[i - 1][j],
                                       dp[i][j - 1]),
                                       dp[i - 1][j - 1] ) + 1
 
                # If not equal, then 
                # it will form a 1x1
                # submatrix
                else:
                    dp[i][j] = 1
 
            # Update result at each (i,j)
            result = max(result, dp[i][j])
             
    return result
 
# Driver Code
a = [[ 2, 2, 3, 3, 4, 4],
     [ 5, 5, 7, 7, 7, 4],
     [ 1, 2, 7, 7, 7, 4],
     [ 4, 4, 7, 7, 7, 4],
     [ 5, 5, 5, 1, 2, 7],
     [ 8, 7, 9, 4, 4, 4]];
 
print(largestKSubmatrix(a))
 
# This code is contributed
# by ChitraNayal


PHP


Javascript


输出:

3

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