📌  相关文章
📜  二进制矩阵中的计数位置在相应的行和列中具有相等的设置位计数

📅  最后修改于: 2021-05-04 23:47:34             🧑  作者: Mango

给定大小为M * N的布尔矩阵mat [] [] 任务是从矩阵中打印索引计数,该矩阵的相应行和列包含相等数量的设置位。

例子:

天真的方法:最简单的解决方法是计算和存储存在于给定矩阵的每一行和每一列的元素中的置位位数。然后,遍历矩阵,对于每个位置,检查行和列的设置位计数是否相等。对于发现以上条件成立的每个索引,增加count 。矩阵完全遍历后,打印count的最终值。

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

高效方法:要优化上述方法,请按照以下步骤操作:

  • 分别初始化大小为NM的两个临时数组row []col []
  • 遍历矩阵mat [] [] 。如果mat [i] [j]等于1 ,则检查每个索引(i,j) 。如果确定为true,则将row [i]col [j]递增1
  • 再次遍历矩阵mat [] [] 。如果row [i]col [j]是否相等请检查每个索引(i,j) 。如果发现为真,则增加计数。
  • 打印count的最终值。

下面是上述方法的实现:

C++14
// C++14 program to implement
// above approach
#include 
using namespace std;
 
// Function to return the count of indices in
// from the given binary matrix having equal
// count of set bits in its row and column
int countPosition(vector> mat)
{
    int n = mat.size();
    int m = mat[0].size();
 
    // Stores count of set bits in
    // coressponding column and row
    vector row(n);
    vector col(m);
 
    // Traverse matrix
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
             
            // Since 1 contains a set bit
            if (mat[i][j] == 1)
            {
                 
                // Update count of set bits
                // for current row and col
                col[j]++;
                row[i]++;
            }
        }
    }
 
    // Stores the count of
    // required indices
    int count = 0;
 
    // Traverse matrix
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
             
            // If current row and column
            // has equal count of set bits
            if (row[i] == col[j])
            {
                count++;
            }
        }
    }
 
    // Return count of
    // required position
    return count;
}
 
// Driver Code
int main()
{
    vector> mat = { { 0, 1 },
                                { 1, 1 } };
 
    cout << (countPosition(mat));
}
 
// This code is contributed by mohit kumar 29


Java
// Java Program to implement
// above approach
 
import java.util.*;
import java.lang.*;
 
class GFG {
 
    // Function to return the count of indices in
    // from the given binary matrix having equal
    // count of set bits in its row and column
    static int countPosition(int[][] mat)
    {
 
        int n = mat.length;
        int m = mat[0].length;
 
        // Stores count of set bits in
        // coressponding column and row
        int[] row = new int[n];
        int[] col = new int[m];
 
        // Traverse matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                // Since 1 contains a set bit
                if (mat[i][j] == 1) {
 
                    // Update count of set bits
                    // for current row and col
                    col[j]++;
                    row[i]++;
                }
            }
        }
 
        // Stores the count of
        // required indices
        int count = 0;
 
        // Traverse matrix
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                // If current row and column
                // has equal count of set bits
                if (row[i] == col[j]) {
                    count++;
                }
            }
        }
 
        // Return count of
        // required position
        return count;
    }
 
    // Driver Code
    public static void main(
        String[] args)
    {
 
        int mat[][] = { { 0, 1 },
                        { 1, 1 } };
 
        System.out.println(
            countPosition(mat));
    }
}


Python3
# Python3 program to implement
# the above approach
 
# Function to return the count
# of indices in from the given
# binary matrix having equal
# count of set bits in its
# row and column
def countPosition(mat):
     
    n = len(mat)
    m = len(mat[0])
  
    # Stores count of set bits in
    # coressponding column and row
    row = [0] * n
    col = [0] * m
  
    # Traverse matrix
    for i in range(n):
        for j in range(m):
              
            # Since 1 contains a set bit
            if (mat[i][j] == 1):
                  
                # Update count of set bits
                # for current row and col
                col[j] += 1
                row[i] += 1
         
    # Stores the count of
    # required indices
    count = 0
  
    # Traverse matrix
    for i in range(n):
        for j in range(m):
              
            # If current row and column
            # has equal count of set bits
            if (row[i] == col[j]):
                count += 1
  
    # Return count of
    # required position
    return count
  
# Driver Code
mat = [ [ 0, 1 ],
        [ 1, 1 ] ]
  
print(countPosition(mat))
 
# This code is contributed by sanjoy_62


C#
// C# Program to implement
// above approach
using System;
class GFG{
 
// Function to return the count of indices in
// from the given binary matrix having equal
// count of set bits in its row and column
static int countPosition(int[,] mat)
{
 
  int n = mat.GetLength(0);
  int m = mat.GetLength(1);
 
  // Stores count of set bits in
  // coressponding column and row
  int[] row = new int[n];
  int[] col = new int[m];
 
  // Traverse matrix
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      // Since 1 contains a set bit
      if (mat[i, j] == 1)
      {
        // Update count of set bits
        // for current row and col
        col[j]++;
        row[i]++;
      }
    }
  }
 
  // Stores the count of
  // required indices
  int count = 0;
 
  // Traverse matrix
  for (int i = 0; i < n; i++)
  {
    for (int j = 0; j < m; j++)
    {
      // If current row and column
      // has equal count of set bits
      if (row[i] == col[j])
      {
        count++;
      }
    }
  }
 
  // Return count of
  // required position
  return count;
}
 
// Driver Code
public static void Main(String[] args)
{
  int [,]mat = {{0, 1},
                {1, 1}};
  Console.WriteLine(countPosition(mat));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2

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