Skip to content
Discussion options

You must be logged in to vote

We need to count the number of submatrices in a given binary matrix that consist entirely of ones. The approach involves dynamically updating the height of consecutive ones in each column for every row and then calculating the number of valid submatrices that end at each row.

Approach

  1. Initialization: Initialize an array h to keep track of the number of consecutive ones ending at each column for the current row. This array is updated for each row in the matrix.
  2. Update Heights: For each row, update the h array. If the current element is 1, increment the height of the corresponding column; otherwise, reset it to 0.
  3. Count Submatrices: For each row, after updating the h array, iterate through…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Aug 21, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants