📌  相关文章
📜  在二进制矩阵中计数 1,其行和列的剩余索引用 0 填充

📅  最后修改于: 2021-09-06 06:35:21             🧑  作者: Mango

给定一个大小为M * N的二进制矩阵mat[][] ,任务是从给定的二进制矩阵中计算1的数量,该矩阵的对应行和列仅在其余索引中由0组成。

例子:

朴素方法:最简单的方法是遍历矩阵并通过遍历其相应的行和列来检查给定矩阵中存在的所有1的给定条件。增加满足条件的所有1计数。最后,将计数打印为所需答案。

时间复杂度: O(M*N 2 )
辅助空间: O(M + N)

高效的方法:上述方法可以基于这样的想法进行优化,即这些行和列的总和仅为1 。请按照以下步骤解决问题:

  1. 初始化两个数组rows[]cols[] ,分别存储矩阵的每行和每列的总和。
  2. 初始化一个变量,比如cnt ,以存储满足给定条件的1的计数。
  3. 遍历每个mat[i][j] = 1的矩阵,检查rows[i]cols[j]是否为1 。如果发现为真,则增加cnt
  4. 完成上述步骤后,打印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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live