给定一个2D二进制矩阵垫[] []的大小为N×M和两个整数A,B中,任务是找到在尺寸A×B或B×A的子矩阵1秒本的数量最少。
例子:
Input:
mat[][] = {{1, 1, 1},
{1, 1, 1},
{1, 1, 1}}
A = 2, B = 1
Output: 2
Explanation: Any submatrix of size 2 X 1 or 1 X 2 will have 2 1s in it.
Input:
mat[][] = {{1, 1, 0, 1, 1, 1, 0, 0},
{0, 1, 0, 1, 1, 1, 1, 1},
{1, 1, 0, 0, 1, 0, 0, 1},
{0, 1, 1, 1, 1, 0, 1, 0},
{0, 1, 1, 0, 1, 1, 0, 1},
{0, 1, 1, 0, 0, 1, 0, 1},
{1, 0, 0, 0, 1, 1, 0, 1},
{0, 1, 1, 0, 1, 1, 1, 1},
{0, 1, 1, 1, 0, 1, 0, 1},
{1, 1, 0, 1, 1, 0, 1, 1}}
A = 4, B = 9
Output: 20
Explanation:
Submatrix from (0, 0) to (8, 3) of dimensions 9 × 4 have 20 1s present in it, which is minimum possible for this matrix.
方法:解决该问题的方法是打印所有可能的尺寸为A * B和B * A的子矩阵,对于每个子矩阵,计算其中存在的1 s的数量。
请按照以下步骤解决给定的问题:
- 初始化变量(例如最小值)以存储最小计数1 s。
- 迭代通过矩阵垫的每个单元(i,j)的[] [],并为每个(I,J):
- 如果i + A小于等于N且j + B小于等于M ,则执行以下操作:
- 初始化一个变量,例如count,将1 s的计数存储在子矩阵{mat [i] [j],mat [i + A] [j + B]}中。
- 遍历子矩阵的每个单元,并将1 s的数量存储在变量计数中。
- 检查计数值是否小于当前最小值。如果发现为真,则更新最小值等于count 。
- 如果i + B小于等于N并且j + A小于等于M ,则执行以下操作:
- 初始化一个变量,例如count,将1 s的计数存储在子矩阵{mat [i] [j],mat [i + B] [j + A]}中。
- 遍历子矩阵的每个单元,并存储1 s的计数。
- 检查计数值是否小于当前最小值。如果发现为真,则更新最小值等于count 。
- 如果i + A小于等于N且j + B小于等于M ,则执行以下操作:
- 最后,打印最小值作为最终结果。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
#define P 51
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
int count1s(int start_i, int start_j,
int end_i, int end_j, int mat[][P])
{
// Stores the number of 1s
// present in current submatrix
int count = 0;
// Traverse the submatrix
for (int x = start_i; x < end_i; x++) {
for (int y = start_j; y < end_j; y++) {
// If mat[x][y] is equal to 1
if (mat[x][y] == 1)
// Increase count by 1
count++;
}
}
// Return the total count of 1s
return count;
}
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
int findMinimumCount(int N, int M, int A, int B,
int mat[][P])
{
// Stores the minimum count of 1s
int minimum = 1e9;
// Iterate i from 0 to N
for (int i = 0; i < N; i++) {
// Iterate j from 0 to M
for (int j = 0; j < M; j++) {
// If a valid sub matrix of size
// A * B from (i, j) is possible
if (i + A <= N && j + B <= M) {
// Count the number of 1s
// present in the sub matrix
// of size A * B from (i, j)
int count
= count1s(i, j, i + A, j + B, mat);
// Update minimum if count is
// less than the current minimum
minimum = min(count, minimum);
}
// If a valid sub matrix of size
// B * A from (i, j) is possible
if (i + B <= N && j + A <= M) {
// Count the number of 1s in the
// sub matrix of size B * A from (i, j)
int count
= count1s(i, j, i + B, j + A, mat);
// Update minimum if count is
// less than the current minimum
minimum = min(count, minimum);
}
}
}
// Return minimum as the final result
return minimum;
}
// Driver Code
int main()
{
// Given Input
int A = 2, B = 2;
int N = 3, M = 4;
int mat[P][P] = { { 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 1, 0, 1, 0 } };
// Function call to find the minimum number
// of 1s in a submatrix of size A * B or B * A
cout << findMinimumCount(N, M, A, B, mat);
}
Java
// Java program for the above approach
class GFG{
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
static int count1s(int start_i, int start_j,
int end_i, int end_j,
int[][] mat)
{
// Stores the number of 1s
// present in current submatrix
int count = 0;
// Traverse the submatrix
for(int x = start_i; x < end_i; x++)
{
for(int y = start_j; y < end_j; y++)
{
// If mat[x][y] is equal to 1
if (mat[x][y] == 1)
// Increase count by 1
count++;
}
}
// Return the total count of 1s
return count;
}
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
static int findMinimumCount(int N, int M, int A,
int B, int[][] mat)
{
// Stores the minimum count of 1s
int minimum = (int) 1e9;
// Iterate i from 0 to N
for(int i = 0; i < N; i++)
{
// Iterate j from 0 to M
for(int j = 0; j < M; j++)
{
// If a valid sub matrix of size
// A * B from (i, j) is possible
if (i + A <= N && j + B <= M)
{
// Count the number of 1s
// present in the sub matrix
// of size A * B from (i, j)
int count = count1s(i, j, i + A,
j + B, mat);
// Update minimum if count is
// less than the current minimum
minimum = Math.min(count, minimum);
}
// If a valid sub matrix of size
// B * A from (i, j) is possible
if (i + B <= N && j + A <= M)
{
// Count the number of 1s in the
// sub matrix of size B * A from (i, j)
int count = count1s(i, j, i + B,
j + A, mat);
// Update minimum if count is
// less than the current minimum
minimum = Math.min(count, minimum);
}
}
}
// Return minimum as the final result
return minimum;
}
// Driver code
public static void main(String[] args)
{
// Given Input
int A = 2, B = 2;
int N = 3, M = 4;
int[][] mat = { { 1, 0, 1, 0 },
{ 0, 1, 0, 1 },
{ 1, 0, 1, 0 } };
// Function call to find the minimum number
// of 1s in a submatrix of size A * B or B * A
System.out.println(findMinimumCount(N, M, A, B, mat));
}
}
// This code is contributed by user_qa7r
Python3
# Python3 program for the above approach
P = 51
# Function to count number of 1s
# present in a sub matrix from
# (start_i, start_j) to (end_i, end_j)
def count1s(start_i, start_j,
end_i, end_j, mat):
# Stores the number of 1s
# present in current submatrix
count = 0
# Traverse the submatrix
for x in range(start_i, end_i):
for y in range(start_j, end_j):
# If mat[x][y] is equal to 1
if (mat[x][y] == 1):
# Increase count by 1
count += 1
# Return the total count of 1s
return count
# Function to find the minimum number of 1s
# present in a sub-matrix of size A * B or B * A
def findMinimumCount(N, M, A, B, mat):
# Stores the minimum count of 1s
minimum = 1e9
# Iterate i from 0 to N
for i in range(N):
# Iterate j from 0 to M
for j in range(M):
# If a valid sub matrix of size
# A * B from (i, j) is possible
if (i + A <= N and j + B <= M):
# Count the number of 1s
# present in the sub matrix
# of size A * B from (i, j)
count = count1s(i, j, i + A, j + B, mat)
# Update minimum if count is
# less than the current minimum
minimum = min(count, minimum)
# If a valid sub matrix of size
# B * A from (i, j) is possible
if (i + B <= N and j + A <= M):
# Count the number of 1s in the
# sub matrix of size B * A from (i, j)
count = count1s(i, j, i + B, j + A, mat)
# Update minimum if count is
# less than the current minimum
minimum = min(count, minimum)
# Return minimum as the final result
return minimum
# Driver Code
if __name__ == "__main__":
# Given Input
A = 2
B = 2
N = 3
M = 4
mat = [[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0]]
# Function call to find the minimum number
# of 1s in a submatrix of size A * B or B * A
print(findMinimumCount(N, M, A, B, mat))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count number of 1s
// present in a sub matrix from
// (start_i, start_j) to (end_i, end_j)
static int count1s(int start_i, int start_j,
int end_i, int end_j,
List> mat)
{
// Stores the number of 1s
// present in current submatrix
int count = 0;
// Traverse the submatrix
for(int x = start_i; x < end_i; x++)
{
for(int y = start_j; y < end_j; y++)
{
// If mat[x][y] is equal to 1
if (mat[x][y] == 1)
// Increase count by 1
count++;
}
}
// Return the total count of 1s
return count;
}
// Function to find the minimum number of 1s
// present in a sub-matrix of size A * B or B * A
static void findMinimumCount(int N, int M, int A, int B,
List> mat)
{
// Stores the minimum count of 1s
int minimum = 1000000;
// Iterate i from 0 to N
for(int i = 0; i < N; i++)
{
// Iterate j from 0 to M
for(int j = 0; j < M; j++)
{
// If a valid sub matrix of size
// A * B from (i, j) is possible
if ((i + A <= N) && (j + B <= M))
{
// Count the number of 1s
// present in the sub matrix
// of size A * B from (i, j)
int count = count1s(i, j, i + A,
j + B, mat);
// Update minimum if count is
// less than the current minimum
minimum = Math.Min(count, minimum);
}
// If a valid sub matrix of size
// B * A from (i, j) is possible
if ((i + B <= N) && (j + A <= M))
{
// Count the number of 1s in the
// sub matrix of size B * A from (i, j)
int count = count1s(i, j, i + B,
j + A, mat);
// Update minimum if count is
// less than the current minimum
minimum = Math.Min(count, minimum);
}
}
}
// Return minimum as the final result
Console.WriteLine(minimum);
}
// Driver Code
public static void Main()
{
// Given Input
int A = 2, B = 2;
int N = 3, M = 4;
List> mat = new List>();
mat.Add(new List(new int[]{1, 0, 1, 0}));
mat.Add(new List(new int[]{0, 1, 0, 1}));
mat.Add(new List(new int[]{1, 0, 1, 0}));
// Function call to find the minimum number
// of 1s in a submatrix of size A * B or B * A
findMinimumCount(N, M, A, B, mat);
}
}
// This code is contributed by ipg2016107
2
时间复杂度: O(N 2 )
辅助空间: O(1)