26(June) Coverage of all Zeros in a Binary Matrix

26. Coverage of All Zeros in a Binary Matrix

The problem can be found at the following link: Question Link

Problem Description

Given a binary matrix having n rows and m columns, your task is to find the sum of the coverage of all zeros in the matrix. The coverage for a particular 0 is defined as the total number of ones around it in the left, right, up, and bottom directions.

Examples:

Input:

matrix = [[0, 1, 0],
          [0, 1, 1],
          [0, 0, 0]]

Output:

6

My Approach

  1. Initialization:

  • Initialize a variable cnt to 0 to keep track of the sum of the coverage of all zeros.

  1. Checking Coverage:

  • Define a helper function checkAndCount(i, j) to check the presence of 1s around the cell at position (i, j). The function will increment cnt for each 1 found to the left, right, above, or below the zero.

  1. Iterating Over the Matrix:

  • Iterate over each element in the matrix using nested loops.

  • If the current element is 0, call the helper function to check its coverage.

  1. Return:

  • Return the final count cnt which represents the total coverage of all zeros in the matrix.

Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(n * m), as we iterate through each element of the matrix.

  • Expected Auxiliary Space Complexity: O(1), as we use only a constant amount of additional space.

Code (C++)

Code (Java)

Code (Python)

Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


📍Visitor Count

Last updated