给定大小为M * N的二进制矩阵mat [] [] ,任务是从给定的二进制矩阵(其对应的行和列仅由0 s组成)中计数1 s的数目,仅在其余索引中。
例子:
Input: mat[][] = {{1, 0, 0}, {0, 0, 1}, {0, 0, 0}}
Output: 2
Explanation:
The only two cells satisfying the conditions are (0, 0) and (1, 2).
Therefore, the count is 2.
Input: mat[][] = {{1, 0}, {1, 1}}
Output: 0
天真的方法:最简单的方法是遍历矩阵,并遍历矩阵的相应行和列,以检查给定条件是否存在于给定矩阵中的所有1 s。满足条件的所有1 s的计数增加。最后,将计数打印为所需答案。
时间复杂度: O(M * N 2 )
辅助空间: O(M + N)
高效的方法:可以基于这样的行和列的总和仅为1的想法来优化上述方法。请按照以下步骤解决问题:
- 初始化两个数组rows []和cols [] ,以分别存储矩阵的每一行和每一列的和。
- 初始化一个变量,例如cnt ,以存储满足给定条件的1 s的计数。
- 遍历每个mat [i] [j] = 1的矩阵,检查row [i]和cols [j]是否为1。如果发现为true,则递增cnt 。
- 完成上述步骤后,打印count的最终值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count required 1s
// from the given matrix
int numSpecial(vector >& mat)
{
// Stores the dimensions of the mat[][]
int m = mat.size(), n = mat[0].size();
int rows[m];
int cols[n];
int i, j;
// Calculate sum of rows
for (i = 0; i < m; i++) {
rows[i] = 0;
for (j = 0; j < n; j++)
rows[i] += mat[i][j];
}
// Calculate sum of columns
for (i = 0; i < n; i++) {
cols[i] = 0;
for (j = 0; j < m; j++)
cols[i] += mat[j][i];
}
// Stores required count of 1s
int cnt = 0;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
// If current cell is 1
// and sum of row and column is 1
if (mat[i][j] == 1 && rows[i] == 1
&& cols[j] == 1)
// Increment count of 1s
cnt++;
}
}
// Return the final count
return cnt;
}
// Driver Code
int main()
{
// Given matrix
vector > mat
= { { 1, 0, 0 }, { 0, 0, 1 }, { 0, 0, 0 } };
// Function Call
cout << numSpecial(mat) << endl;
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to count required 1s
// from the given matrix
static int numSpecial(int [][]mat)
{
// Stores the dimensions of the mat[][]
int m = mat.length;
int n = mat[0].length;
int []rows = new int[m];
int []cols = new int[n];
int i, j;
// Calculate sum of rows
for(i = 0; i < m; i++)
{
rows[i] = 0;
for(j = 0; j < n; j++)
rows[i] += mat[i][j];
}
// Calculate sum of columns
for(i = 0; i < n; i++)
{
cols[i] = 0;
for(j = 0; j < m; j++)
cols[i] += mat[j][i];
}
// Stores required count of 1s
int cnt = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
// If current cell is 1
// and sum of row and column is 1
if (mat[i][j] == 1 &&
rows[i] == 1 &&
cols[j] == 1)
// Increment count of 1s
cnt++;
}
}
// Return the final count
return cnt;
}
// Driver Code
public static void main(String[] args)
{
// Given matrix
int [][]mat = { { 1, 0, 0 },
{ 0, 0, 1 },
{ 0, 0, 0 } };
// Function call
System.out.print(numSpecial(mat) + "\n");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to count required 1s
# from the given matrix
def numSpecial(mat):
# Stores the dimensions
# of the mat
m = len(mat)
n = len(mat[0])
rows = [0] * m
cols = [0] * n
i, j = 0, 0
# Calculate sum of rows
for i in range(m):
rows[i] = 0
for j in range(n):
rows[i] += mat[i][j]
# Calculate sum of columns
for i in range(n):
cols[i] = 0
for j in range(m):
cols[i] += mat[j][i]
# Stores required count of 1s
cnt = 0
for i in range(m):
for j in range(n):
# If current cell is 1
# and sum of row and column is 1
if (mat[i][j] == 1 and
rows[i] == 1 and
cols[j] == 1):
# Increment count of 1s
cnt += 1
# Return the final count
return cnt
# Driver Code
if __name__ == '__main__':
# Given matrix
mat = [ [ 1, 0, 0 ],
[ 0, 0, 1 ],
[ 0, 0, 0 ] ]
# Function call
print(numSpecial(mat))
# This code is contributed by Amit Katiyar
C#
// C# program for the above approach
using System;
class GFG{
// Function to count required 1s
// from the given matrix
static int numSpecial(int [,]mat)
{
// Stores the dimensions of the [,]mat
int m = mat.GetLength(0);
int n = mat.GetLength(1);
int []rows = new int[m];
int []cols = new int[n];
int i, j;
// Calculate sum of rows
for(i = 0; i < m; i++)
{
rows[i] = 0;
for(j = 0; j < n; j++)
rows[i] += mat[i, j];
}
// Calculate sum of columns
for(i = 0; i < n; i++)
{
cols[i] = 0;
for(j = 0; j < m; j++)
cols[i] += mat[j, i];
}
// Stores required count of 1s
int cnt = 0;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
// If current cell is 1 and
// sum of row and column is 1
if (mat[i, j] == 1 &&
rows[i] == 1 &&
cols[j] == 1)
// Increment count of 1s
cnt++;
}
}
// Return the readonly count
return cnt;
}
// Driver Code
public static void Main(String[] args)
{
// Given matrix
int [,]mat = { { 1, 0, 0 },
{ 0, 0, 1 },
{ 0, 0, 0 } };
// Function call
Console.Write(numSpecial(mat) + "\n");
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
2
时间复杂度: O(M * N)
辅助空间: O(M + N)