给定一个整数矩阵mat [ ][ ]维,任务是从给定矩阵中找到最大可能的方阵,其中 AND 值最大。
AND value of a matrix is defined as the value obtained after performing bitwise AND operation on all elements of the matrix.
例子:
Input: mat [ ][ ] = {{2, 3, 3}, {2, 3, 3}, {2, 2, 2}}
Output: 4
Explanation:
Given square submatrix has AND value 2.
The submatrix
{{3, 3}
{3, 3}}
of size 4 has maximum AND value 3. All other square submatrices of size 4 have AND value 2.
Input: mat [ ][ ] =
{{9, 9, 9, 8},
{9, 9, 9, 6},
{9, 9, 9, 3},
{2, 2, 2, 2}}
Output: 9
Explanation:
The submatrix of size 9
{{9, 9, 9},
{9, 9, 9},
{9, 9, 9}}
have maximum AND value 9.
天真的方法:
从给定矩阵生成所有方形子矩阵。初始化一个变量answer来存储子矩阵的最大值和值,另一个变量count来存储子矩阵中元素的数量。打印与从所有平方子矩阵获得的最大 AND 值答案相对应的count最大值。
有效的方法:
按照以下步骤优化上述解决方案:
- 为了最大化 & 值,我们需要找到一个只包含矩阵中最大元素的子矩阵。这是因为矩阵中最大可能的 AND 值是矩阵中存在的最大元素。
- 找出矩阵中存在的最大可能值。
- 使用动态规划方法获得仅由最大矩阵元素填充的最大尺寸子矩阵。
- 创建一个辅助dp[][]使得 dp[i][j] 存储最大可能的正方形子矩阵 mat[i][j] 可以是其中的一部分,使得该子矩阵的 AND 值等于 mat[i] [j]。
- 递推关系如下:
If mat[i][j] is equal to {mat[i-1][j], mat[i][j-1], mat[i-1][j-1]} then consider all the three values as a square submatrix and update DP[i][j] as:
DP[i][j] = min(DP[i-1][j], DP[i][j-1], DP[i-1][j-1]) + 1
Otherwise,
DP[i][j] = 1
The answer would be the maximum of all DP [i][j]
- 最后,遍历 dp[][] 矩阵并为每个 mat[i][j] 找到最大的 dp[i][j] 等于数组中的最大元素。
以下是上述方法的实现:
C++
// C++ program to find
// the length of longest
// possible square submatrix
// with maximum AND value
// from the given matrix
#include
using namespace std;
// Function to calculate and
// return the length of
// square submatrix with
// maximum AND value
int MAX_value(vector > arr)
{
// Extract dimensions
int row = arr.size();
int col = arr[0].size();
// Auxiliary array
int dp[row][col];
// Initialize auxiliary array
memset(dp, sizeof(dp), 0);
// c: Stores the maximum
// value in the matrix
// p: Stores the number
// of elements in the
// submatrix having
// maximum AND value
int i = 0, j = 0;
int c = arr[0][0], p = 0;
int d = row;
// Iterate over the matrix
// to fill the auxiliary
// matrix
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
// Find the max element in the
// matrix side by side
if (c < arr[i][j]) {
c = arr[i][j];
}
// Fill first row and
// column with 1's
if (i == 0 || j == 0) {
dp[i][j] = 1;
}
else {
// For every cell, check if
// the elements at the left,
// top and top left cells
// from the current cell
// are equal or not
if (arr[i - 1][j - 1] == arr[i][j]
&& arr[i - 1][j] == arr[i][j]
&& arr[i][j - 1] == arr[i][j]) {
// Store the minimum possible
// submatrix size these
// elements are part of
dp[i][j]
= min(dp[i - 1][j - 1],
min(dp[i - 1][j],
dp[i][j - 1]))
+ 1;
}
else {
// Store 1 otherwise
dp[i][j] = 1;
}
}
}
}
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
// checking maximum value
if (arr[i][j] == c) {
// If the maximum AND
// value occurs more
// than once
if (p < dp[i][j]) {
// Update the maximum
// size of submatrix
p = dp[i][j];
}
}
}
}
// final output
return p * p;
}
// Driver Program
int main()
{
vector > arr
= { { 9, 9, 3, 3, 4, 4 },
{ 9, 9, 7, 7, 7, 4 },
{ 1, 2, 7, 7, 7, 4 },
{ 4, 4, 7, 7, 7, 4 },
{ 5, 5, 1, 1, 2, 7 },
{ 2, 7, 1, 1, 4, 4 } };
cout << MAX_value(arr) << endl;
return 0;
}
Java
// Java program to find the length
// of longest possible square
// submatrix with maximum AND
// value from the given matrix
import java.util.*;
class GFG{
// Function to calculate and return
// the length of square submatrix
// with maximum AND value
static int MAX_value(int [][]arr)
{
// Extract dimensions
int row = arr.length;
int col = arr[0].length;
// Auxiliary array
int [][]dp = new int[row][col];
// c: Stores the maximum
// value in the matrix
// p: Stores the number
// of elements in the
// submatrix having
// maximum AND value
int i = 0, j = 0;
int c = arr[0][0], p = 0;
int d = row;
// Iterate over the matrix
// to fill the auxiliary
// matrix
for(i = 0; i < d; i++)
{
for(j = 0; j < d; j++)
{
// Find the max element in
// the matrix side by side
if (c < arr[i][j])
{
c = arr[i][j];
}
// Fill first row and
// column with 1's
if (i == 0 || j == 0)
{
dp[i][j] = 1;
}
else
{
// For every cell, check if the
// elements at the left, top and
// top left cells from the current
// cell are equal or not
if (arr[i - 1][j - 1] == arr[i][j] &&
arr[i - 1][j] == arr[i][j] &&
arr[i][j - 1] == arr[i][j])
{
// Store the minimum possible
// submatrix size these
// elements are part of
dp[i][j] = Math.min(dp[i - 1][j - 1],
Math.min(dp[i - 1][j],
dp[i][j - 1])) + 1;
}
else
{
// Store 1 otherwise
dp[i][j] = 1;
}
}
}
}
for(i = 0; i < d; i++)
{
for(j = 0; j < d; j++)
{
// Checking maximum value
if (arr[i][j] == c)
{
// If the maximum AND
// value occurs more
// than once
if (p < dp[i][j])
{
// Update the maximum
// size of submatrix
p = dp[i][j];
}
}
}
}
// Final output
return p * p;
}
// Driver code
public static void main(String[] args)
{
int [][]arr = { { 9, 9, 3, 3, 4, 4 },
{ 9, 9, 7, 7, 7, 4 },
{ 1, 2, 7, 7, 7, 4 },
{ 4, 4, 7, 7, 7, 4 },
{ 5, 5, 1, 1, 2, 7 },
{ 2, 7, 1, 1, 4, 4 } };
System.out.print(MAX_value(arr) + "\n");
}
}
// This code contributed by amal kumar choubey
Python3
# Python3 program to find the length
# of longest possible square submatrix
# with maximum AND value from the given
# matrix
# Function to calculate and return the
# length of square submatrix with
# maximum AND value
def MAX_value(arr):
# Extract dimensions
row = len(arr)
col = len(arr)
# Auxiliary array
# Initialize auxiliary array
dp = [[0 for i in range(col)]
for j in range(row)]
# c: Stores the maximum
# value in the matrix
# p: Stores the number
# of elements in the
# submatrix having
# maximum AND value
i, j = 0, 0
c, p = arr[0][0], 0
d = row
# Iterate over the matrix
# to fill the auxiliary
# matrix
for i in range(d):
for j in range(d):
# Find the max element in the
# matrix side by side
if (c < arr[i][j]):
c = arr[i][j]
# Fill first row and
# column with 1's
if (i == 0 or j == 0):
dp[i][j] = 1
else:
# For every cell, check if
# the elements at the left,
# top and top left cells
# from the current cell
# are equal or not
if (arr[i - 1][j - 1] == arr[i][j] and
arr[i - 1][j] == arr[i][j] and
arr[i][j - 1] == arr[i][j]):
# Store the minimum possible
# submatrix size these
# elements are part of
dp[i][j] = min(dp[i - 1][j - 1],
min(dp[i - 1][j],
dp[i][j - 1])) + 1
else:
# Store 1 otherwise
dp[i][j] = 1
for i in range(d):
for j in range(d):
# Checking maximum value
if (arr[i][j] == c):
# If the maximum AND
# value occurs more
# than once
if (p < dp[i][j]):
# Update the maximum
# size of submatrix
p = dp[i][j]
# Final output
return p * p
# Driver Code
arr = [ [ 9, 9, 3, 3, 4, 4 ],
[ 9, 9, 7, 7, 7, 4 ],
[ 1, 2, 7, 7, 7, 4 ],
[ 4, 4, 7, 7, 7, 4 ],
[ 5, 5, 1, 1, 2, 7 ],
[ 2, 7, 1, 1, 4, 4 ]]
print(MAX_value(arr))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find the length
// of longest possible square
// submatrix with maximum AND
// value from the given matrix
using System;
class GFG{
// Function to calculate and return
// the length of square submatrix
// with maximum AND value
static int MAX_value(int [,]arr)
{
// Extract dimensions
int row = arr.GetLength(0);
int col = arr.GetLength(1);
// Auxiliary array
int [,]dp = new int[row, col];
// c: Stores the maximum
// value in the matrix
// p: Stores the number
// of elements in the
// submatrix having
// maximum AND value
int i = 0, j = 0;
int c = arr[0, 0], p = 0;
int d = row;
// Iterate over the matrix
// to fill the auxiliary
// matrix
for(i = 0; i < d; i++)
{
for(j = 0; j < d; j++)
{
// Find the max element in
// the matrix side by side
if (c < arr[i, j])
{
c = arr[i, j];
}
// Fill first row and
// column with 1's
if (i == 0 || j == 0)
{
dp[i, j] = 1;
}
else
{
// For every cell, check if the
// elements at the left, top and
// top left cells from the current
// cell are equal or not
if (arr[i - 1, j - 1] == arr[i, j] &&
arr[i - 1, j] == arr[i, j] &&
arr[i, j - 1] == arr[i, j])
{
// Store the minimum possible
// submatrix size these
// elements are part of
dp[i, j] = Math.Min(dp[i - 1, j - 1],
Math.Min(dp[i - 1, j],
dp[i, j - 1])) + 1;
}
else
{
// Store 1 otherwise
dp[i, j] = 1;
}
}
}
}
for(i = 0; i < d; i++)
{
for(j = 0; j < d; j++)
{
// Checking maximum value
if (arr[i, j] == c)
{
// If the maximum AND
// value occurs more
// than once
if (p < dp[i, j])
{
// Update the maximum
// size of submatrix
p = dp[i, j];
}
}
}
}
// Final output
return p * p;
}
// Driver code
public static void Main(String[] args)
{
int [,]arr = { { 9, 9, 3, 3, 4, 4 },
{ 9, 9, 7, 7, 7, 4 },
{ 1, 2, 7, 7, 7, 4 },
{ 4, 4, 7, 7, 7, 4 },
{ 5, 5, 1, 1, 2, 7 },
{ 2, 7, 1, 1, 4, 4 } };
Console.Write(MAX_value(arr) + "\n");
}
}
// This code is contributed by gauravrajput1
Javascript
4
时间复杂度: O(N 2 )
辅助空间: O(N 2 )