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

📅  最后修改于: 2021-10-27 06:54:13             🧑  作者: Mango

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

例子:

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

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

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

  • 分别初始化大小为NM 的两个临时数组row[]col[]
  • 遍历矩阵mat[][] 。检查每个索引(i, j) ,如果mat[i][j]等于1或不。如果发现为真,则将row[i]col[j]增加1
  • 再次遍历矩阵mat[][] 。检查每个索引(i, j) ,如果row[i]col[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
    // corresponding 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
        // corresponding 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
    # corresponding 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
  // corresponding 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程