给定一个N×M的整数矩阵和一个整数K ,任务是找到最大平方子矩阵(S x S)的大小,以使给定矩阵的所有平方子矩阵都具有一个总和小于K。
例子:
Input: K = 30
mat[N][M] = {{1, 2, 3, 4, 6},
{5, 3, 8, 1, 2},
{4, 6, 7, 5, 5},
{2, 4, 8, 9, 4} };
Output: 2
Explanation:
All Sub-matrices of size 2 x 2
have sum less than 30
Input : K = 100
mat[N][M] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 },
{ 13, 14, 15, 16 } };
Output: 3
Explanation:
All Sub-matrices of size 3 x 3
have sum less than 100
朴素的方法基本的解决方案是选择子矩阵的大小S ,找到该大小的所有子矩阵,并检查所有子矩阵的总和是否小于给定的总和,而这可以通过计算子矩阵的总和来改善。使用这种方法的矩阵。因此,任务是选择最大可能的大小以及每个可能的子矩阵的开始和结束位置。因此,整体时间复杂度将为O(N 3 )。
下面是上述方法的实现:
C++
// C++ implementation to find the
// maximum size square submatrix
// such that their sum is less than K
#include
using namespace std;
// Size of matrix
#define N 4
#define M 5
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
void preProcess(int mat[N][M],
int aux[N][M])
{
// Loop to copy the first row
// of the matrix into the aux matrix
for (int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
int sumQuery(int aux[N][M], int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res +
aux[tli - 1][tlj - 1];
return res;
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
int maximumSquareSize(int mat[N][M], int K)
{
int aux[N][M];
preProcess(mat, aux);
// Loop to choose the size of matrix
for (int i = min(N, M); i >= 1; i--) {
bool satisfies = true;
// Loop to find the sum of the
// matrix of every possible submatrix
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x + i - 1 <= N - 1 &&
y + i - 1 <= M - 1) {
if (sumQuery(aux, x, y,
x + i - 1, y + i - 1) > K)
satisfies = false;
}
}
}
if (satisfies == true)
return (i);
}
return 0;
}
// Driver Code
int main()
{
int K = 30;
int mat[N][M] = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
cout << maximumSquareSize(mat, K);
return 0;
}
Java
// Java implementation to find the
// maximum size square submatrix
// such that their sum is less than K
class GFG{
// Size of matrix
static final int N = 4;
static final int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [][]mat,
int [][]aux)
{
// Loop to copy the first row
// of the matrix into the aux matrix
for (int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [][]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res +
aux[tli - 1][tlj - 1];
return res;
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [][]mat, int K)
{
int [][]aux = new int[N][M];
preProcess(mat, aux);
// Loop to choose the size of matrix
for (int i = Math.min(N, M); i >= 1; i--) {
boolean satisfies = true;
// Loop to find the sum of the
// matrix of every possible submatrix
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x + i - 1 <= N - 1 &&
y + i - 1 <= M - 1) {
if (sumQuery(aux, x, y,
x + i - 1, y + i - 1) > K)
satisfies = false;
}
}
}
if (satisfies == true)
return (i);
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
int K = 30;
int mat[][] = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
System.out.print(maximumSquareSize(mat, K));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation to find the
# maximum size square submatrix
# such that their sum is less than K
# Size of matrix
N = 4
M = 5
# Function to preprocess the matrix
# for computing the sum of every
# possible matrix of the given size
def preProcess(mat, aux):
# Loop to copy the first row
# of the matrix into the aux matrix
for i in range (M):
aux[0][i] = mat[0][i]
# Computing the sum column-wise
for i in range (1, N):
for j in range (M):
aux[i][j] = (mat[i][j] +
aux[i - 1][j])
# Computing row wise sum
for i in range (N):
for j in range (1, M):
aux[i][j] += aux[i][j - 1]
# Function to find the sum of a
# submatrix with the given indices
def sumQuery(aux, tli, tlj, rbi, rbj):
# Overall sum from the top to
# right corner of matrix
res = aux[rbi][rbj]
# Removing the sum from the top
# corer of the matrix
if (tli > 0):
res = res - aux[tli - 1][rbj]
# Remove the overlapping sum
if (tlj > 0):
res = res - aux[rbi][tlj - 1]
# Add the sum of top corner
# which is substracted twice
if (tli > 0 and tlj > 0):
res = (res +
aux[tli - 1][tlj - 1])
return res
# Function to find the maximum
# square size possible with the
# such that every submatrix have
# sum less than the given sum
def maximumSquareSize(mat, K):
aux = [[0 for x in range (M)]
for y in range (N)]
preProcess(mat, aux)
# Loop to choose the size of matrix
for i in range (min(N, M), 0, -1):
satisfies = True
# Loop to find the sum of the
# matrix of every possible submatrix
for x in range (N):
for y in range (M) :
if (x + i - 1 <= N - 1 and
y + i - 1 <= M - 1):
if (sumQuery(aux, x, y,
x + i - 1,
y + i - 1) > K):
satisfies = False
if (satisfies == True):
return (i)
return 0
# Driver Code
if __name__ == "__main__":
K = 30
mat = [[1, 2, 3, 4, 6],
[5, 3, 8, 1, 2],
[4, 6, 7, 5, 5],
[2, 4, 8, 9, 4]]
print( maximumSquareSize(mat, K))
# This code is contributed by Chitranayal
C#
// C# implementation to find the
// maximum size square submatrix
// such that their sum is less than K
using System;
public class GFG{
// Size of matrix
static readonly int N = 4;
static readonly int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [,]mat,
int [,]aux)
{
// Loop to copy the first row
// of the matrix into the aux matrix
for (int i = 0; i < M; i++)
aux[0,i] = mat[0,i];
// Computing the sum column-wise
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
aux[i,j] = mat[i,j] +
aux[i - 1,j];
// Computing row wise sum
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
aux[i,j] += aux[i,j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [,]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi,rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1,rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi,tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res +
aux[tli - 1,tlj - 1];
return res;
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [,]mat, int K)
{
int [,]aux = new int[N,M];
preProcess(mat, aux);
// Loop to choose the size of matrix
for (int i = Math.Min(N, M); i >= 1; i--) {
bool satisfies = true;
// Loop to find the sum of the
// matrix of every possible submatrix
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x + i - 1 <= N - 1 &&
y + i - 1 <= M - 1) {
if (sumQuery(aux, x, y,
x + i - 1, y + i - 1) > K)
satisfies = false;
}
}
}
if (satisfies == true)
return (i);
}
return 0;
}
// Driver Code
public static void Main(String[] args)
{
int K = 30;
int [,]mat = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
Console.Write(maximumSquareSize(mat, K));
}
}
// This code contributed by PrinciRaj1992
C++
// C++ implementation to find the
// maximum size square submatrix
// such that their sum is less than K
#include
using namespace std;
// Size of matrix
#define N 4
#define M 5
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
void preProcess(int mat[N][M],
int aux[N][M])
{
// Loop to copy the first row
// of the matrix into the aux matrix
for (int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
int sumQuery(int aux[N][M], int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1][tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
bool check(int mid, int aux[N][M],
int K)
{
bool satisfies = true;
// Iterating throught all possible
// submatrices of given size
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1) {
if (sumQuery(aux, x, y,
x + mid - 1, y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
int maximumSquareSize(int mat[N][M],
int K)
{
int aux[N][M];
preProcess(mat, aux);
// Search space
int low = 1, high = min(N, M);
int mid;
// Binary search for size
while (high - low > 1) {
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K)) {
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
int main()
{
int K = 30;
int mat[N][M] = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
cout << maximumSquareSize(mat, K);
return 0;
}
Java
// Java implementation to find the
// maximum size square submatrix
// such that their sum is less than K
class GFG{
// Size of matrix
static final int N = 4;
static final int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [][]mat,
int [][]aux)
{
// Loop to copy the first row of
// the matrix into the aux matrix
for(int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for(int i = 1; i < N; i++)
for(int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for(int i = 0; i < N; i++)
for(int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [][]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1][tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
static boolean check(int mid, int [][]aux,
int K)
{
boolean satisfies = true;
// Iterating throught all possible
// submatrices of given size
for(int x = 0; x < N; x++)
{
for(int y = 0; y < M; y++)
{
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1)
{
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [][]mat,
int K)
{
int [][]aux = new int[N][M];
preProcess(mat, aux);
// Search space
int low = 1, high = Math.min(N, M);
int mid;
// Binary search for size
while (high - low > 1)
{
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K))
{
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
public static void main(String[] args)
{
int K = 30;
int [][]mat = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
System.out.print(maximumSquareSize(mat, K));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to find the
# maximum size square submatrix
# such that their sum is less than K
# Function to preprocess the matrix
# for computing the sum of every
# possible matrix of the given size
def preProcess(mat, aux):
# Loop to copy the first row
# of the matrix into the aux matrix
for i in range(5):
aux[0][i] = mat[0][i]
# Computing the sum column-wise
for i in range(1, 4):
for j in range(5):
aux[i][j] = (mat[i][j] +
aux[i - 1][j])
# Computing row wise sum
for i in range(4):
for j in range(1, 5):
aux[i][j] += aux[i][j - 1]
return aux
# Function to find the sum of a
# submatrix with the given indices
def sumQuery(aux, tli, tlj, rbi, rbj):
# Overall sum from the top to
# right corner of matrix
res = aux[rbi][rbj]
# Removing the sum from the top
# corer of the matrix
if (tli > 0):
res = res - aux[tli - 1][rbj]
# Remove the overlapping sum
if (tlj > 0):
res = res - aux[rbi][tlj - 1]
# Add the sum of top corner
# which is substracted twice
if (tli > 0 and tlj > 0):
res = res + aux[tli - 1][tlj - 1]
return res
# Function to check whether square
# sub matrices of size mid satisfy
# the condition or not
def check(mid, aux, K):
satisfies = True
# Iterating throught all possible
# submatrices of given size
for x in range(4):
for y in range(5):
if (x + mid - 1 <= 4 - 1 and
y + mid - 1 <= 5 - 1):
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K):
satisfies = False
return True if satisfies == True else False
# Function to find the maximum
# square size possible with the
# such that every submatrix have
# sum less than the given sum
def maximumSquareSize(mat, K):
aux = [[0 for i in range(5)]
for i in range(4)]
aux = preProcess(mat, aux)
# Search space
low , high = 1, min(4, 5)
mid = 0
# Binary search for size
while (high - low > 1):
mid = (low + high) // 2
# Check if the mid satisfies
# the given condition
if (check(mid, aux, K)):
low = mid
else:
high = mid
if (check(high, aux, K)):
return high
return low
# Driver Code
if __name__ == '__main__':
K = 30
mat = [ [ 1, 2, 3, 4, 6 ],
[ 5, 3, 8, 1, 2 ],
[ 4, 6, 7, 5, 5 ],
[ 2, 4, 8, 9, 4 ] ]
print(maximumSquareSize(mat, K))
# This code is contributed by mohit kumar 29
C#
// C# implementation to find the
// maximum size square submatrix
// such that their sum is less than K
using System;
class GFG{
// Size of matrix
static readonly int N = 4;
static readonly int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [,]mat,
int [,]aux)
{
// Loop to copy the first row of
// the matrix into the aux matrix
for(int i = 0; i < M; i++)
aux[0, i] = mat[0, i];
// Computing the sum column-wise
for(int i = 1; i < N; i++)
for(int j = 0; j < M; j++)
aux[i, j] = mat[i, j] +
aux[i - 1, j];
// Computing row wise sum
for(int i = 0; i < N; i++)
for(int j = 1; j < M; j++)
aux[i, j] += aux[i, j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [,]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi, rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1, rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi, tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1, tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
static bool check(int mid, int [,]aux,
int K)
{
bool satisfies = true;
// Iterating throught all possible
// submatrices of given size
for(int x = 0; x < N; x++)
{
for(int y = 0; y < M; y++)
{
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1)
{
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [,]mat,
int K)
{
int [,]aux = new int[N, M];
preProcess(mat, aux);
// Search space
int low = 1, high = Math.Min(N, M);
int mid;
// Binary search for size
while (high - low > 1)
{
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K))
{
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
public static void Main(String[] args)
{
int K = 30;
int [,]mat = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
Console.Write(maximumSquareSize(mat, K));
}
}
// This code is contributed by Rajput-Ji
输出:
2
- 时间复杂度: O(N 3 )
- 辅助空间: O(N 2 )
高效方法:关键观察是,如果s边的正方形是满足条件的最大尺寸,则所有小于其尺寸的尺寸都将满足条件。使用此方法,我们可以将每一步的搜索空间减少一半,而这正是Binary Search的想法。下面是该方法步骤的说明:
- 搜索空间:此问题的搜索空间为[1,min(N,M)]。那就是二进制搜索的搜索空间被定义为–
low = 1
high = min(N, M)
- 下一个搜索空间:在每次迭代中,找到搜索空间的中间,然后最后,检查所有具有该大小的子数组的总和小于K。如果该大小的所有子数组的总和小于K。那么下一个搜索空间可能在中间的右边。否则,下一个搜索空间可能会在中间的左侧。那比中间少。
- 情况1:条件是当大小为mid的所有子数组的总和小于K时。然后 –
if checkSubmatrix(mat, mid, K):
low = mid + 1
- 情况2:当大小为mid的所有子数组的总和大于K时的情况。然后 –
if not checkSubmatrix(mat, mid, K):
high = mid - 1
下面是上述方法的实现:
C++
// C++ implementation to find the
// maximum size square submatrix
// such that their sum is less than K
#include
using namespace std;
// Size of matrix
#define N 4
#define M 5
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
void preProcess(int mat[N][M],
int aux[N][M])
{
// Loop to copy the first row
// of the matrix into the aux matrix
for (int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
int sumQuery(int aux[N][M], int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1][tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
bool check(int mid, int aux[N][M],
int K)
{
bool satisfies = true;
// Iterating throught all possible
// submatrices of given size
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1) {
if (sumQuery(aux, x, y,
x + mid - 1, y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
int maximumSquareSize(int mat[N][M],
int K)
{
int aux[N][M];
preProcess(mat, aux);
// Search space
int low = 1, high = min(N, M);
int mid;
// Binary search for size
while (high - low > 1) {
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K)) {
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
int main()
{
int K = 30;
int mat[N][M] = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
cout << maximumSquareSize(mat, K);
return 0;
}
Java
// Java implementation to find the
// maximum size square submatrix
// such that their sum is less than K
class GFG{
// Size of matrix
static final int N = 4;
static final int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [][]mat,
int [][]aux)
{
// Loop to copy the first row of
// the matrix into the aux matrix
for(int i = 0; i < M; i++)
aux[0][i] = mat[0][i];
// Computing the sum column-wise
for(int i = 1; i < N; i++)
for(int j = 0; j < M; j++)
aux[i][j] = mat[i][j] +
aux[i - 1][j];
// Computing row wise sum
for(int i = 0; i < N; i++)
for(int j = 1; j < M; j++)
aux[i][j] += aux[i][j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [][]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi][rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1][rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi][tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1][tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
static boolean check(int mid, int [][]aux,
int K)
{
boolean satisfies = true;
// Iterating throught all possible
// submatrices of given size
for(int x = 0; x < N; x++)
{
for(int y = 0; y < M; y++)
{
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1)
{
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [][]mat,
int K)
{
int [][]aux = new int[N][M];
preProcess(mat, aux);
// Search space
int low = 1, high = Math.min(N, M);
int mid;
// Binary search for size
while (high - low > 1)
{
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K))
{
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
public static void main(String[] args)
{
int K = 30;
int [][]mat = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
System.out.print(maximumSquareSize(mat, K));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation to find the
# maximum size square submatrix
# such that their sum is less than K
# Function to preprocess the matrix
# for computing the sum of every
# possible matrix of the given size
def preProcess(mat, aux):
# Loop to copy the first row
# of the matrix into the aux matrix
for i in range(5):
aux[0][i] = mat[0][i]
# Computing the sum column-wise
for i in range(1, 4):
for j in range(5):
aux[i][j] = (mat[i][j] +
aux[i - 1][j])
# Computing row wise sum
for i in range(4):
for j in range(1, 5):
aux[i][j] += aux[i][j - 1]
return aux
# Function to find the sum of a
# submatrix with the given indices
def sumQuery(aux, tli, tlj, rbi, rbj):
# Overall sum from the top to
# right corner of matrix
res = aux[rbi][rbj]
# Removing the sum from the top
# corer of the matrix
if (tli > 0):
res = res - aux[tli - 1][rbj]
# Remove the overlapping sum
if (tlj > 0):
res = res - aux[rbi][tlj - 1]
# Add the sum of top corner
# which is substracted twice
if (tli > 0 and tlj > 0):
res = res + aux[tli - 1][tlj - 1]
return res
# Function to check whether square
# sub matrices of size mid satisfy
# the condition or not
def check(mid, aux, K):
satisfies = True
# Iterating throught all possible
# submatrices of given size
for x in range(4):
for y in range(5):
if (x + mid - 1 <= 4 - 1 and
y + mid - 1 <= 5 - 1):
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K):
satisfies = False
return True if satisfies == True else False
# Function to find the maximum
# square size possible with the
# such that every submatrix have
# sum less than the given sum
def maximumSquareSize(mat, K):
aux = [[0 for i in range(5)]
for i in range(4)]
aux = preProcess(mat, aux)
# Search space
low , high = 1, min(4, 5)
mid = 0
# Binary search for size
while (high - low > 1):
mid = (low + high) // 2
# Check if the mid satisfies
# the given condition
if (check(mid, aux, K)):
low = mid
else:
high = mid
if (check(high, aux, K)):
return high
return low
# Driver Code
if __name__ == '__main__':
K = 30
mat = [ [ 1, 2, 3, 4, 6 ],
[ 5, 3, 8, 1, 2 ],
[ 4, 6, 7, 5, 5 ],
[ 2, 4, 8, 9, 4 ] ]
print(maximumSquareSize(mat, K))
# This code is contributed by mohit kumar 29
C#
// C# implementation to find the
// maximum size square submatrix
// such that their sum is less than K
using System;
class GFG{
// Size of matrix
static readonly int N = 4;
static readonly int M = 5;
// Function to preprocess the matrix
// for computing the sum of every
// possible matrix of the given size
static void preProcess(int [,]mat,
int [,]aux)
{
// Loop to copy the first row of
// the matrix into the aux matrix
for(int i = 0; i < M; i++)
aux[0, i] = mat[0, i];
// Computing the sum column-wise
for(int i = 1; i < N; i++)
for(int j = 0; j < M; j++)
aux[i, j] = mat[i, j] +
aux[i - 1, j];
// Computing row wise sum
for(int i = 0; i < N; i++)
for(int j = 1; j < M; j++)
aux[i, j] += aux[i, j - 1];
}
// Function to find the sum of a
// submatrix with the given indices
static int sumQuery(int [,]aux, int tli,
int tlj, int rbi, int rbj)
{
// Overall sum from the top to
// right corner of matrix
int res = aux[rbi, rbj];
// Removing the sum from the top
// corer of the matrix
if (tli > 0)
res = res - aux[tli - 1, rbj];
// Remove the overlapping sum
if (tlj > 0)
res = res - aux[rbi, tlj - 1];
// Add the sum of top corner
// which is substracted twice
if (tli > 0 && tlj > 0)
res = res + aux[tli - 1, tlj - 1];
return res;
}
// Function to check whether square
// sub matrices of size mid satisfy
// the condition or not
static bool check(int mid, int [,]aux,
int K)
{
bool satisfies = true;
// Iterating throught all possible
// submatrices of given size
for(int x = 0; x < N; x++)
{
for(int y = 0; y < M; y++)
{
if (x + mid - 1 <= N - 1 &&
y + mid - 1 <= M - 1)
{
if (sumQuery(aux, x, y,
x + mid - 1,
y + mid - 1) > K)
satisfies = false;
}
}
}
return (satisfies == true);
}
// Function to find the maximum
// square size possible with the
// such that every submatrix have
// sum less than the given sum
static int maximumSquareSize(int [,]mat,
int K)
{
int [,]aux = new int[N, M];
preProcess(mat, aux);
// Search space
int low = 1, high = Math.Min(N, M);
int mid;
// Binary search for size
while (high - low > 1)
{
mid = (low + high) / 2;
// Check if the mid satisfies
// the given condition
if (check(mid, aux, K))
{
low = mid;
}
else
high = mid;
}
if (check(high, aux, K))
return high;
return low;
}
// Driver Code
public static void Main(String[] args)
{
int K = 30;
int [,]mat = { { 1, 2, 3, 4, 6 },
{ 5, 3, 8, 1, 2 },
{ 4, 6, 7, 5, 5 },
{ 2, 4, 8, 9, 4 } };
Console.Write(maximumSquareSize(mat, K));
}
}
// This code is contributed by Rajput-Ji
输出:
2
性能分析:
- 时间复杂度: O(N 2 * log(N))
- 辅助空间: O(N 2 )
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。