Skip to content
Discussion options

You must be logged in to vote

We need to find the smallest rectangle with sides parallel to the grid that can cover all the 1's in a given binary matrix. The solution involves determining the boundaries of the rectangle by identifying the extreme positions (topmost, bottommost, leftmost, and rightmost) of the 1's in the grid.

Approach

  1. Initialize Boundaries: Start by initializing variables to keep track of the minimum and maximum row and column indices where 1's are found. Set the initial minimum row and column to large values (larger than any possible index in the grid) and the maximum row and column to -1.
  2. Traverse the Grid: Iterate through each cell in the grid. For each cell that contains a 1, update the boundaries:

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kovatz
Comment options

kovatz Aug 22, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Aug 22, 2025
Maintainer Author

Answer selected by kovatz
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