Mid/SeniorArrayMatrix

Find the Minimum Area to Cover All Ones I

You are given a 2D binary array grid. Find a rectangle with horizontal and vertical sides with the smallest area, such that all the 1s in grid lie inside this rectangle.

Return the minimum possible area of the rectangle.

Example 1
0 1 0
1 0 1
Inputgrid = [[0,1,0],[1,0,1]]
Output6
The smallest rectangle has a height of 2 and a width of 3, so it has an area of 2 * 3 = 6.
Example 2
1 0
0 0
Inputgrid = [[1,0],[0,0]]
Output1
The smallest rectangle has both height and width 1, so its area is 1 * 1 = 1.

Constraints

  • 1 <= grid.length, grid[i].length <= 1000
  • grid[i][j] is either 0 or 1.
  • The input is generated such that there is at least one 1 in grid.

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate